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

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.

Related

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.

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.

EF Code First - creating database - Login failed for user

I originally had a EF code first set up that was connecting to an existing database. This was working fine.
I then made a couple changes to a POCO and decided to have code first generate the new database for me.
Getting error: Cannot open database \"MyDatabase\" requested by the login. The login failed.\r\nLogin failed for user 'DOMAIN\username'.
I deleted the old database, but I did not change the connection string:
<add name="MyDatabaseContext" connectionString="Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True;" providerName="System.Data.SqlClient" />
I have a sql server 2008 instance on my local machine and my domain username is in "sysadmin" role.
I tried various Database initializers and I get the same error for all. It is failing on the first query call, but code first does not create the database. I can point the connection string to a copy of the old database (before changes) and it will run fine, except that it is my old schema even though I specified the DropCreateDatabaseAlways initializer. This is not making sense, and not following what I experienced on my home machine working with code first.
Using Visual studio 2012 and EF5.
I need to be able to have code first generate a new database. What is going on?
Found out I had a static constructor in my DbContext class, which was overriding the database initializer.
static MyDbContext()
{
Database.SetInitializer<MyDbContext>(null);
}
Code first reverse engineer will put this in so you don't overwrite your existing database. When I switched I wasn't thinking about it.
Hope this will help someone else out.

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.