Entity Framework is trying to create a database, but its not what I want

前端 未结 2 418
无人及你
无人及你 2021-02-08 19:08

I have a small asp.net mvc4 application (working fine in my local machine), that uses entity framework v4.1.0.0 with ADO.net DbContext Generator.(SQL Server 2008 r2)

I a

相关标签:
2条回答
  • 2021-02-08 19:17

    If you are using EF Migrations, this is what you set for it:

    public sealed class DbConfiguration : DbMigrationsConfiguration<DatabaseContext>
    {
        public DbConfiguration()
        {
            AutomaticMigrationsEnabled = false;
        }
    }
    

    But this doesn't answer the question on EF Code First itself. If the database already exists, then EF will not try to create it. So you just need to point it to an existing database. And to make sure the connection string name is the same as the name of the database context. If it is not, you need to provide it to it with some overrides:

    public class DatabaseContext : DbContext
    {
        public DatabaseContext() 
          : base(ApplicationParameters.ConnectionStringName)
        {
        }
    
        public DatabaseContext(string connectionStringName)
          : base(connectionStringName)
        {
        }
    
    }
    
    0 讨论(0)
  • 2021-02-08 19:24

    put this code into the Application_Start() method of Global.asax or constructor on your DbContext class

    Database.SetInitializer<MyContext>(null);
    

    If you want to recreate database when POCO domains are changed, use following code instead of above

    Database.SetInitializer<MyContext>(new DropCreateDatabaseIfModelChanges<MyContext>());
    
    0 讨论(0)
提交回复
热议问题