EF Code First - Database Creation - Login Error for User

Initially, I had the first EF code that connected to an existing database. It works great.

Then I made a couple of changes to POCO and decided that the code would first generate a new database for me.

Error while retrieving: Cannot open the database "MyDatabase" requested at login. 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 an instance of sql server 2008 on my local computer and my domain username is in the role of "sysadmin".

I tried different database initializers and I get the same error for everyone. It does not work the first time the request is called, but at first the code does not create the database. I can point the connection string to a copy of the old database (before the changes), and it will work fine, except that this is my old scheme, although I specified the DropCreateDatabaseAlways initializer. This does not make sense and does not follow what I experienced on my home machine working with code in the first place.

Using Visual studio 2012 and EF5.

I need to have code to generate a new database. What's happening?

+6
source share
1 answer

I had a static constructor in my DbContext class that redefined the database initializer.

 static MyDbContext() { Database.SetInitializer<MyDbContext>(null); } 

The first reverse code engineer will put this so that you do not overwrite the existing database. When I switched, I did not think about it.

Hope this helps someone else.

+8
source

All Articles