I would like to prevent my program from dropping the database each time I run the debugger during the developing stage. I want to do this without the usual seeding the database. Since I will be using the Import and Export Data wizard, I would like to use this method of populating my database.
我想防止我的程序每次在开发阶段运行调试器时丢失数据库。我想在不播数据库的情况下做这个。由于我将使用导入和导出数据向导,所以我希望使用这种方法填充数据库。
Is there a method to prevent the program from dropping the database?
有什么方法可以防止程序删除数据库吗?
Here's more information that I hope will help:
以下是我希望能提供的更多信息:
My Initializer
我的初始化
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);
}
}
My application start
我的应用程序开始
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);
}
My DBContext
我DBContext
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; }
}
My connection string
我的连接字符串
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
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来DropCreateDatabaseAlways,不管模型是否更改,我都只加载数据库。我将使用导入和导出向导填充数据库。
Thanks for helping!
谢谢你的帮助!
2 个解决方案
#1
1
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来DropCreateDatabaseAlways,不管模型是否更改,我都只加载数据库。我将使用导入和导出向导填充数据库
So don't call SetInitializer. Just use the connection string to open the DB. BTW, DropCreateDatabaseIfModelChanges as the name implies, only drops/creates the DB when the schema changes.
所以不要叫SetInitializer。只需使用连接字符串来打开DB。顺便说一句,dropcreatedatabaseifmodelas会随名称而更改,只有在模式更改时才会删除/创建DB。
That's how traditional DB first works.
传统的DB首先就是这样工作的。
#2
1
To elaborate on RickAnd's answer, for future searchers looking for an answer.
要详细阐述里克德的答案,为未来的搜索者寻找答案。
I assume your full initializer class declaration is ..
我假设您的完整初始化器类声明是。
public class ApplicationDbInitializer : DropCreateDatabaseAlways<SchoolInDB>
You simply need to replace this with...
你只需要用…
public class ApplicationDbInitializer : CreateDatabaseIfNotExists<SchoolInDB>
That way your database will still be initialised with your seed values, but won't be dropped each time you start the application.
这样,您的数据库仍将使用种子值进行初始化,但不会在每次启动应用程序时被删除。
#1
1
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来DropCreateDatabaseAlways,不管模型是否更改,我都只加载数据库。我将使用导入和导出向导填充数据库
So don't call SetInitializer. Just use the connection string to open the DB. BTW, DropCreateDatabaseIfModelChanges as the name implies, only drops/creates the DB when the schema changes.
所以不要叫SetInitializer。只需使用连接字符串来打开DB。顺便说一句,dropcreatedatabaseifmodelas会随名称而更改,只有在模式更改时才会删除/创建DB。
That's how traditional DB first works.
传统的DB首先就是这样工作的。
#2
1
To elaborate on RickAnd's answer, for future searchers looking for an answer.
要详细阐述里克德的答案,为未来的搜索者寻找答案。
I assume your full initializer class declaration is ..
我假设您的完整初始化器类声明是。
public class ApplicationDbInitializer : DropCreateDatabaseAlways<SchoolInDB>
You simply need to replace this with...
你只需要用…
public class ApplicationDbInitializer : CreateDatabaseIfNotExists<SchoolInDB>
That way your database will still be initialised with your seed values, but won't be dropped each time you start the application.
这样,您的数据库仍将使用种子值进行初始化,但不会在每次启动应用程序时被删除。