如何使用模型第一种方式时播种数据?如何使用、第一种、模型、方式

2023-09-03 10:01:08 作者:抱走请留言

所以我学习MVC3和EF4。 我试过code第一种方法,但它是太混乱了我..我可以创建类没有问题,但与外键和相互之间的关系,处理困难的部分来了。

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.

在我的模型是创造,它创造,我对我的SQL防爆preSS数据库执行一个SQL我。完成和完成的。

After my model is create, it creates a SQL for me which I execute against my SQL Express database. Done, and done.

现在我想在我的表中的数据。当然,我可以再补充他们使用服务器资源管理器,但最有可能我会做出改变我的模型,我走。而不断更新的数据库。所以,我不能让手动输入数据。我知道,如果你用code首先,你可以推导出 DropCreateDatabaseIfModelChanges 并覆盖方法。

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.

但是我怎么做到这一点与模型第一种方法? 我有以下的code:

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?

推荐答案

您可以有这样的事情:

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
{


}

然后,在的Application_Start()您拨打:

Database.SetInitializer(new MySeedData());

在你的情况,你可以尝试创建DbSets(使用模型的第一类),手动并尝试使用上面的code堵塞了。这是一种模式首先+ $ C C首先$组成的混合系统。

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; }
}

添加到这一点:CreateDatabaseIfNotExists<(Of &LT;(小于'TContext>)>)>