Login fails when recreating database with Code First

Posted by Mun on Stack Overflow See other posts from Stack Overflow or by Mun
Published on 2012-12-10T15:50:40Z Indexed on 2012/12/10 17:03 UTC
Read the original article Hit count: 183

I'm using ASP.NET Entity Framework's Code First to create my database from the model, and the login seems to fail when the database needs to be recreated after the model changes.

In Global.asax, I've got the following:

protected void Application_Start()
{
    Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EntriesContext>());

    // ...
}

In my controller, I've got the following:

public ActionResult Index()
{
    // This is just to force the database to be created
    var context = new EntriesContext();
    var all = (from e in context.Entries select e).ToList();
}

When the database doesn't exist, it is created with no problems. However, when I make a change to the model, rebuild and refresh, I get the following error:

Login failed for user 'sa'.

My connection string looks like this:

<add name="EntriesContext"
     connectionString="Server=(LOCAL);Database=MyDB;User Id=sa;Password=password"
     providerName="System.Data.SqlClient" />

The login definitely works as I can connect to the server and the database from Management Studio using these credentials.

If I delete the database manually, everything works correctly and the database is recreated as expected with the schema reflecting the changes made to the model.

It seems like either the password or access to the database is being lost.

Is there something else I need to do to get this working?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about entity-framework