I am having trouble creating DB in SQL Server Express with Entity Framework Core 2.0 - entity-framework-core

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.

Related

EF7 Upgrade - Create Database not working

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?

How to switch from LocalDB instance to SQL Express?

I created an SQL Express database and built an Entity Data Model off of it using the wizard, then used the Repository Pattern to generate a helper class that does my querying for me.
Unfortunately what I didn't realize is that Code First creates a localdb copy and basically ALL my add/removes now go to this ethereal location that I can't find so I can't run queries off the data in SQL Express. Now I know the data is being saved because I'm able to check the underlying model through breakpoints and queries during page load with using(Context = new Context()) { /query stuff/ }, but I don't know how to switch from the underlying model back to my SqlExpress instance.
Any thoughts?
LocalDb is just another sql server database, which is started by Visual Studio. It's data most of the time are in %USER% directory and you can open it with Sql Server Management Studio. To switch your project to your sql express instance, you need to change the connection string, that's all.

Entity Framework 4.3 with SQL Server Express migration exception

I've set up a (remote) SQL Server Express for my ASP.NET MVC with Entity Framework 4.3 project. On my local machine using SQL Compact everything works fine. When I try to connect to the SQL Express server I got the following error on the call migrator.Update():
The INSERT permission was denied on the object '__MigrationHistory', database 'MyDataBase', schema 'dbo'.
On the server I've done the following:
Created a user with SQL Server credentials
Created a database called MyDataBase
Any ideas?
The DB roles db_denydatareader,db_denydatawriter must be unchecked for the created user.
I was getting this error too. To fix it I added db_owner to my user's "database role membership", and checked the db_owner check box under "schemas owned by this user" (SSMS 2008).

Why does Entity give me an "SSDL fragment is required" statement with a remote DB, but not SQL Express?

Similar to this question, I am trying to run an Entity CodeFirst project. I'm using the CreateDatabaseIfNotExists initializer. I am a sysadmin on the box the connect string points to.
If I change (or remove) the connection string, Entity will create the database on my local SQLExpress instance. Why won't it create the database on my remote instance? And, furthermore, why does it seem to require an artifact of data-first? This is a brand new project that is using nothing but code-first.
Using EF4.2. Remote machine is SQL2k8.

EF 4.1 CF: CREATE DATABASE permission denied in database 'master'

Entity Framework 4.1 Code First works great with SQLEXPRESS on localhost. However, I'm now ready to connect to a regular SQL 2008 server.
I created a new database "NewEfDatabase".
Then changed my "ApplicationServices" connectionString in Web.config to point to my new database with integrated security.
But then I get this error:
"CREATE DATABASE permission denied in database 'master'."
So...
a) What permissions does EF 4.1 CF need on said SQL server to do its work?
b) Can I setup an empty database on SQL 2008 for EF 4.1 CF, or do I have to let it do all that work for me? (I'm not sure my DBA would appreciate letting my EF app have rights to do anything outside a particular database)
Did you make sure to set your Database Initializer to null in your code:
Database.SetInitializer<MyDbContext>(null);
All built-in implementations if the initializer may try to drop or create a new database. The error you get indicate that EF tried to drop/create a database but hasn't the right to do so in your SQL Server instance. The line above is the only option to avoid this generally and is suited (I think even absolutely necessary) for live environments anyway where you don't want accidental deletion (due to model changes or something).
Setup a login for your application within SQL server. Give that user dbcreator permission (by right clicking on the login, go to server roles, and check the "dbcreator" checkbox)
Try not to used the windows-intergrated authorization, like what I replied in this post: EF Code First - {"CREATE DATABASE permission denied in database 'master'."}.
It worked for me.