EF7 Upgrade - Create Database not working - entity-framework-core

I started to upgrade my .NET 5/6 applications to .NET 7, I also updated from EF6 to EF7, which seems to be more difficult than expected :-(
I managed to find out that there were breaking changes in the new SqlClient (5.0.1) library, which now forces you to turn off encryption or trust the server certificate:
Encrypt = false; or TrustServerCertificate = yes
After I have set these properties in the connection string, I was again able to connect to my database.
BUT, I am using a code first approach, this means my app is creating / migrating the database if it does not exist or needs migration.
I am now running into the issue that my app is not able to create an empty database anymore. I am still getting an exception
Microsoft.Data.SqlClient.SqlException
Cannot open database "XXX" requested by the login. The login failed.
Login failed for user 'YYY\ZZZ'.
This only happens when I am trying to create a new database.
If I create an empty database (no scheme) manually on my SQL Server Express, all works fine, the scheme gets created and migrations applied. The issue is only the initial creation of the database itself.
Application and SQL Server Express are running on the same machine, SQL Server runs in mixed authentication mode. "Trusted Connection" is enabled in my connection string which looks like this:
Server =.\\SQLExpress; Database = TEST_DB; Trusted_Connection = Yes; Connection Timeout = 5; Encrypt = false;
Before I updated to .NET 7 everything worked fine with the exact same connection string.
Any ideas?

Related

Prisma DB Can't connect to AWS RDS

I have a nextjs project that's using prismaDB for the ORM. I'm able to connect just fine to my local postgres db but I'm getting this error when running npx prisma migrate.
Error: P1001: Can't reach database server at db-name.*.us-west-2.rds.amazonaws.com:5432.
schema.prisma:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
//url = "postgresql://master_username:master_password#aws_host:5432/db_name"
}
The RDS db is currently public and I'm positive that I've copied over the RDS credentials correctly. There doesn't seem to be anything I should be including for the connection to work but I'm not getting any other info as to why I can't reach the db server.
Seems like you have to replace db-name.*.us-west-2.rds.amazonaws.com with the name of your actual database, unless you replaced it for the purpose of asking this question. Specifically the part where it says db-name.*.
Docs: https://www.prisma.io/docs/reference/api-reference/error-reference#common
P1001 indicates that it couldn't find the database given the connection string, NOT necessarily that the credentials you provided were wrong. Make sure you're specifying the correct database name/host and whatever else you need to make it work for AWS.
Somehow I was able to connect to RDS after deleting and creating a new DB for the third time. I confirmed connection through pgAdmin then tried it again my app deployed to vercel.

EF Core migration to remote database

I have a working app and database locally. I want to setup a remote database for the remote application.
I thought forcing the environment variable would to it:
dotnet ef database update -- --environment Production
But it says "...The database is already up to date." Checking the remote database, it's still empty.
Now, the appsettings.json and appsettings.Development.json files appear to be working correctly. I confirmed my local application connects to my local database, and my remote application connects to my remote database.
Given that the connection strings are correct in the settings, how do I tell EF to do the thing again but for the other database? I feel like I'm missing something obvious.
So I was just using the wrong environment declaration. Run this before the update:
$env:ASPNETCORE_ENVIRONMENT = 'Production'

I am having trouble creating DB in SQL Server Express with Entity Framework Core 2.0

I am going through the MvcMusicStore tutorial on the www.asp.net website.
I am able to use Entity Framework Core 2.0 and migrations to create a DB in localDb.
Here is the connection string from that in appsettings.json:
"MvcMovieContext": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-19840e0a-5fb4-409c-9f38-7f2946cd3937;Trusted_Connection=True;MultipleActiveResultSets=true",
When ever I do these tutorials I usually at this point once the DB is created and everything works switch to SQL Server Express, because this is how I actually develop my projects.
So Entity Framework checks the new connection string, doesn't see the DB exists, and creates the new DB based on the Model.
But I can't seem to get the connection string to SQL Express working.
Here is what I have tried so far:
"MvcMovieContext": "Server=MyComputerName\\SQLEXPRESS;Database=MvcMovie;Trusted_Connection=True;MultipleActiveResultSets=true"
and
"MvcMovieContext": "Data Source=.\\SQLEXPRESS;Initial Catalog=MvcMovie;Integrated Security=True;MultipleActiveResultSets=true"
Both give the same result. The dreaded message:
SqlException: Cannot open database "MvcMovie" requested by the login. The login failed.
Login failed for user 'Domain\smiller'.
I installed SQL Server Express in Mixed Mode so Windows Authentication works and I made my 'Domain\smiller' account a DbCreater in Server Roles.
Just can't seem to get it.
I needed to run:
Update-Database
I went through this last year:
Why is dotnet ef not able to create a database against SQL Server Express?
It seems I never learn.

Firebird ADO.NET provider 5.0.5.0 to Firebird 3.0 can not connect except sysdba

I am trying connect to firebird 3.0 throught latest version of ADO.NET provider 5.0.5.0 from my simple test .net application. When i used sysdba user connection is made OK, but if i used other user then connection failed with classic FBException :
Your user name and password are not defined. Ask your database administrator to set up a Firebird login.
"initial catalog=c:\\Database\\Data.fdb;data source=localhost;user id=sysdba;password=sysPass;port number=3050" = OK
"initial catalog=c:\\Database\\Data.fdb;data source=localhost;user id=michal;password=micPass;port number=3050" = failed
But if i am trying connect throught IBExpert or isql , then connection is made OK and i can get data from tables.
Previously i used Firebird 2.1 and then i upgraded to Firebird 3.0 superclassic and get ADO.NET provieder from nuget.Actually i do not need SRP ecryption so I disabled WireCrypt etc. and use Legacy authentication (Firebird 3.0 release notes page 117). Then i created my new user via IBExpert (sysdba was created during installation firebird server).
I do not need migrate users from previous firebird version so I skiped Upgrading a v.2.x Security Database steps.
It is strange that i can connect myUser via IBExpert or isql, but not via ADO.NET provider.
It seems to me unlikely that ADO.NET provider has bug of this type (everyone needed connect via user), so I guess that i have some bad configuration.
I had some bad configurations and I need set correctly:
In firebird.conf, there has to be: WireCrypt = Enabled (not disabled)
Create user account older way (in my case via IBExpert)
The password have to max. length 8 chars (previously you can have more chars and Firebird is omit)

EF7: error connecting to a local db database

I'm trying to create a new asp.net core app which uses EF7 to access a local db database and I'm having some problems with the connection string. I've installed the local db service and I have already created several instances:
PS C:\Users\luisabreu> sqllocaldb info
MSSQLLocalDB
ProjectsV12
ProjectsV13
v11.0
MSSQLLocalDB is running v12:
PS C:\Users\luisabreu> sqllocaldb info mssqllocaldb
Name: MSSQLLocalDB
Version: 12.0.2000.8
Shared name:
Owner: GRM\luisabreu
Auto-create: Yes
State: Running
Last start time: 05/04/2016 08:46:17
Instance pipe name: np:\\.\pipe\LOCALDB#518FD662\tsql\query
This is the connection string I'm passing to EF:
{
"usersCnnString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=UsersInfo;Integrated Security=True"
}
Btw, I've already tried this one too:
"usersCnnString": "Server=(localdb)\\mssqllocaldb;Database=UsersInfo;Trusted_Connection=True"
And here's the setup code I'm using for setting up ED dependencies:
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<UsersContext>(options => options.UseSqlServer(Configuration["usersCnnString"]));
Whenever I try to access the database,I end up with the following error:
System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid.)
I'm surely doing something wrong, but what?
thanks!
Luis
Well, it was something simpler...don't forget to kill iis express whenever you change the config of an ASP.NET Core app so that you'll get the correct config data when you re-run the app. The problem was that when I pasted the path:
Server=(localdb)\\mssqllocaldb...
into the json file, VS 2015 automatically escaped the string, turning it into:
Server=(localdb)\\\\mssqllocaldb...
Yes, I've noticed it, but only after trying to load the app on the browser. I corrected the path but didn't kill the IIS Express instance I was using to test my app. So, the error message was absolutely correct because it was still using the old path (which was trying to reach the mssqllocaldb instance running on a localdb server).
Again, stupid error. Hoping that my mistake will save someone else's 30 mins in the future...
if I create a project with user accounts using VS 2015 with RC1 the project template generates the connection string like this:
{
"Server=(localdb)\\mssqllocaldb;Database=thedbnamehere;Trusted_Connection=True;MultipleActiveResultSets=true"
}
I would try making it closer to that, but also wondering if for some reason UsersInfo might not be a valid name, could be some keyword clash
I noticed that vs uses the "userSecretsId" from project.json as the dbname, not sure if that is required but might be worth trying