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

前端 未结 2 419
无人及你
无人及你 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条回答
  •  梦毁少年i
    2021-02-08 19:17

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

    public sealed class DbConfiguration : DbMigrationsConfiguration
    {
        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)
        {
        }
    
    }
    

提交回复
热议问题