Entity Framework Code-First(18):Turn off DB Initializer

追憶似水流年發表於2016-07-05

Turn off DB Initializer in Code-First:

You can also turn off the DB initializer of your application. Suppose, for the production environment, you don't want to lose existing data, then you can turn off the initializer, as shown in the following:

public class SchoolDBContext: DbContext 
{
    public SchoolDBContext() : base("SchoolDBConnectionString")
    {            
        //Disable initializer
        Database.SetInitializer<SchoolDBContext>(null);
    }
    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }
}

 

You can also turn off the initializer in the configuration file, for example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>    
    <add key="DatabaseInitializerForType SchoolDataLayer.SchoolDBContext, SchoolDataLayer"
            value="Disabled" />
    </appSettings>
</configuration>

 

相關文章