使用Model First方法时如何种子数据? [英] How to seed data when using Model First approach?
问题描述
我尝试了代码第一种方法,但是对我来说太混乱了。我可以创建类没有问题,但是处理外键和彼此之间的关系时,硬件部分来了。
但是我已经和模型一起走了。这样,我可以直观地设计它,并查看关系的位置。
在创建模型之后,它将为我创建一个针对我的SQL SQL数据库。完成并完成。
现在我想要我的表中的数据。当然,我可以把它们添加到使用服务器浏览器中,但很可能我会在我的模型中进行更改。并不断更新数据库。所以我不能手动输入数据。我知道如果你先使用代码,你可以导出 DropCreateDatabaseIfModelChanges
并覆盖 seed
方法。
然而,如何使用模型第一种方法来做到这一点?
我有以下代码:
public class DatabaseInitializer:IDatabaseInitializer< BettingContext> {
public void InitializeDatabase(BettingContext context){
var teams = new List< Team> {
new Team {Name =Toronto Maple Leafs,League =NHL},
new Team {Name =Boston Bruins,League =NHL},
new Team {Name =Vancouver Canucks,League =NHL},
new Team {Name =Nashville Predators,League =NHL},
new Team {Name =Montreal Canadiens League =NHL},
};
}
}
当然,在我的全局文件中:
protected void Application_Start()
{
Database.SetInitializer< BettingContext>(new DatabaseInitializer());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
那么现在什么?如何告诉它运行方法?我做错了什么?
你可以这样做:
public $ MyCode =
{
protected override void Seed(YourDataBaseContextClass context)
{
//在这里创建对象并将其添加到您的上下文DBSets ...
}
}
public class YourDataBaseContextClass:DbContext
{
}
然后,在 Application_Start()
中调用:
Database.SetInitializer(new MySeedData());在您的情况下,您可以尝试手动创建DbSets(使用您的模型的第一类)并尝试插入它使用上面的代码。这是一个混合的Model First + Code First。 public class FourthCoffeeWebContext:DbContext
{
public DbSet< Category>分类{get;组; }
public DbSet< Product>产品{get;组; }
}
添加到: CreateDatabaseIfNotExists<(Of<(<'TContext>)>)>
So I am learning MVC3 and EF4.
I tried the code first method but it was too confusing for me.. I can create the classes no problem, but the hard part comes when dealing with foreign keys and the relationships between each other.
But I've gone with model first. This way I can visually design it and see where the relationships are.
After my model is create, it creates a SQL for me which I execute against my SQL Express database. Done, and done.
Now I want data in my tables. Of course I can just add them in using server explorer, but most likely I will be making changes to my model as I go along. And keep updating the database. So I can't keep manually entering data. I know if you use code first you can derive the DropCreateDatabaseIfModelChanges
and override the seed
method.
However how do I do this with model first approach?
I have the following code:
public class DatabaseInitializer : IDatabaseInitializer<BettingContext> {
public void InitializeDatabase(BettingContext context) {
var teams = new List<Team> {
new Team { Name="Toronto Maple Leafs", League="NHL"},
new Team { Name="Boston Bruins", League="NHL"},
new Team { Name="Vancouver Canucks", League="NHL"},
new Team { Name="Nashville Predators", League="NHL"},
new Team { Name="Montreal Canadiens", League="NHL"},
};
}
}
Of course and in my global file:
protected void Application_Start()
{
Database.SetInitializer<BettingContext>(new DatabaseInitializer());
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
so now what? How do I tell it to run the method? What am I doing wrong?
解决方案 You can have something like this:
public class MySeedData : DropCreateDatabaseIfModelChanges<YourDataBaseContextClass>
{
protected override void Seed(YourDataBaseContextClass context)
{
// Create objects here and add them to your context DBSets...
}
}
public class YourDataBaseContextClass : DbContext
{
}
Then, within Application_Start()
you call:
Database.SetInitializer(new MySeedData());
In your case, you could try creating DbSets (using your model first classes) manually and try to plug it using the code above. It's kind of a mix of Model First + Code First.
public class FourthCoffeeWebContext : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
}
Adding to this: CreateDatabaseIfNotExists<(Of <(<'TContext>)>)>
这篇关于使用Model First方法时如何种子数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!