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>
时间: 2024-11-01 19:35:29