How do I handle situations in which I need pre-existing data before the app is started or right after the database is generated. For example, I have a list of countries in which I'd like to load into the database after code-first generates it. How do I do this?
我如何处理在应用程序启动或数据库生成之前就需要预先存在的数据的情况。例如,我有一个国家列表,我希望在代码-first生成后将其加载到数据库中。我该怎么做呢?
App is structured as follows:
App的结构如下:
Repository > Service > WebMVC
存储库>服务> WebMVC。
The xml is in the WebMVC
project.
xml在WebMVC项目中。
3 个解决方案
#1
67
You create custom initializer, which inherits from DropCreateDatabaseIfModelChanges
or DropCreateDatabaseAlways
interface. Like:
您可以创建自定义初始化器,它继承自DropCreateDatabaseIfModelChanges或DropCreateDatabaseAlways接口。如:
public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<-YourDbContext->
And then you overwrite Seed method like:
然后重写种子方法,比如:
protected override void Seed(YourDbContext context)
Whole example might look like:
整个例子可能是这样的:
public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<EntitiesContext>
{
protected override void Seed(EntitiesContext context)
{
List<Role> roles = new List<Role>
{
new Role {Id=1, Title="Admin"},
new Role {Id=2, Title="ProjectManager"},
new Role {Id=3, Title="Developer"}
};
// add data into context and save to db
foreach (Role r in roles)
{
context.Roles.Add(r);
}
context.SaveChanges();
}
}
Edit: After setting this up, you have to set up Initializer too, as Ladislav Mrnka mentioned.
编辑:设置完这个之后,您还必须设置初始化器,正如Ladislav Mrnka提到的。
Database.SetInitializer(new EntitiesContextInitializer());
ie.: in Global.asax:
ie。:在Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Database.SetInitializer(new EntitiesContextInitializer());
}
Don't forget to add using System.Data.Entity;
.....
不要忘记使用System.Data.Entity添加;.....
#2
10
You must create custom database initializer derived for example from DropCreateDatabaseIfModelChanges
and fill data in overriden Seed
method. Then you must use Database.SetInitializer
to set your new initializer when application starts. Here is example (from CTP5) used to create custom index in the database.
您必须创建自定义的数据库初始化器,例如从DropCreateDatabaseIfModelChanges派生的,并在overriden Seed方法中填充数据。然后必须使用数据库。SetInitializer在应用程序启动时设置新的初始化器。下面是用于在数据库中创建自定义索引的示例(来自CTP5)。
#3
1
For an example see the new MVC / Entity Framework tutorial series at http://www.asp.net/entity-framework/tutorials#Using%20MVC Both #1 and #4 show initializer classes.
例如,请参见http://www.asp.net/entityframework/tutorials的使用%20MVC #1和#4显示初始化类的新MVC /实体框架教程系列。
#1
67
You create custom initializer, which inherits from DropCreateDatabaseIfModelChanges
or DropCreateDatabaseAlways
interface. Like:
您可以创建自定义初始化器,它继承自DropCreateDatabaseIfModelChanges或DropCreateDatabaseAlways接口。如:
public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<-YourDbContext->
And then you overwrite Seed method like:
然后重写种子方法,比如:
protected override void Seed(YourDbContext context)
Whole example might look like:
整个例子可能是这样的:
public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<EntitiesContext>
{
protected override void Seed(EntitiesContext context)
{
List<Role> roles = new List<Role>
{
new Role {Id=1, Title="Admin"},
new Role {Id=2, Title="ProjectManager"},
new Role {Id=3, Title="Developer"}
};
// add data into context and save to db
foreach (Role r in roles)
{
context.Roles.Add(r);
}
context.SaveChanges();
}
}
Edit: After setting this up, you have to set up Initializer too, as Ladislav Mrnka mentioned.
编辑:设置完这个之后,您还必须设置初始化器,正如Ladislav Mrnka提到的。
Database.SetInitializer(new EntitiesContextInitializer());
ie.: in Global.asax:
ie。:在Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Database.SetInitializer(new EntitiesContextInitializer());
}
Don't forget to add using System.Data.Entity;
.....
不要忘记使用System.Data.Entity添加;.....
#2
10
You must create custom database initializer derived for example from DropCreateDatabaseIfModelChanges
and fill data in overriden Seed
method. Then you must use Database.SetInitializer
to set your new initializer when application starts. Here is example (from CTP5) used to create custom index in the database.
您必须创建自定义的数据库初始化器,例如从DropCreateDatabaseIfModelChanges派生的,并在overriden Seed方法中填充数据。然后必须使用数据库。SetInitializer在应用程序启动时设置新的初始化器。下面是用于在数据库中创建自定义索引的示例(来自CTP5)。
#3
1
For an example see the new MVC / Entity Framework tutorial series at http://www.asp.net/entity-framework/tutorials#Using%20MVC Both #1 and #4 show initializer classes.
例如,请参见http://www.asp.net/entityframework/tutorials的使用%20MVC #1和#4显示初始化类的新MVC /实体框架教程系列。