我想防止我的程序在开发阶段每次运行调试器时删除数据库。我想在不进行通常的数据库播种的情况下执行此操作。由于我将使用导入和导出数据向导,因此我想使用这种方法来填充我的数据库。
有没有办法防止程序删除数据库?
以下是更多信息,希望对您有所帮助:
我的初始化程序
DropCreateDatabaseIfModelChanges<SchoolInDB>
{
protected override void Seed(SchoolInDB context)
{
context.Parents.Add(new Parent { Name = "Mary Lawson", Phone = "949-999-9999", Notes = "Please see IEP " });
base.Seed(context);
}
}
我的应用程序启动
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseAlways<SchoolIn.Models.SchoolInDB>());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
我的数据库上下文
public class SchoolInDB : DbContext
{
public DbSet<Course> Courses { get; set; }
public DbSet<Teacher> Teachers { get; set; }
public DbSet<Parent> Parents { get; set; }
public DbSet<PdfReport> PdfReports { get; set; }
public DbSet<CourseProgress> CourseProgresses { get; set; }
public DbSet<ParentContact> ParentContacts { get; set; }
public DbSet<RedAlert> RedAlerts { get; set; }
public DbSet<Student> Students { get; set; }
public DbSet<Assignment> Assignments { get; set; }
}
我的连接字符串
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
我想我想避免使用 SetInitializer 来 DropCreateDatabaseAlways 并仅加载数据库,无论模型是否更改。我将使用导入和导出向导来填充数据库。
感谢您的帮助!
最佳答案
block 引用>I guess I want to avoid using the SetInitializer to DropCreateDatabaseAlways and just load the database whether the models changes or not. I'll be using the Import and Export Wizard to populate the database
所以不要调用SetInitializer。只需使用连接字符串打开数据库即可。顺便说一句,DropCreateDatabaseIfModelChanges 顾名思义,仅在架构更改时删除/创建数据库。
这就是传统数据库最初的工作原理。
关于asp.net-mvc - 如何防止在不做种的情况下重新创建我的数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9447550/