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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Entity Framework Code-First(18):Turn off DB Initializer相关的知识,希望对你有一定的参考价值。

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>

 

以上是关于Entity Framework Code-First(18):Turn off DB Initializer的主要内容,如果未能解决你的问题,请参考以下文章

Entity Framework Code-First(23):Entity Framework Power Tools

Entity Framework怎么GroupBY多个字段

entity framework 公共类

Entity Framework Fluent API

初步了解Entity Framework

Entity Framework 中的in操作实例