database 实体框架代码优先数据库中的默认数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5655841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:09:24  来源:igfitidea点击:

Entity Framework Code-first default data in database

databaseentity-frameworkinitializationef-code-firstentity-framework-4.1

提问by Shawn Mclean

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?

我如何处理在应用程序启动之前或数据库刚生成之后我需要预先存在的数据的情况。例如,我有一个国家列表,我想在代码优先生成后将其加载到数据库中。我该怎么做呢?

App is structured as follows:

应用程序结构如下:

Repository > Service > WebMVC

Repository > Service > WebMVC

The xml is in the WebMVCproject.

xml 在WebMVC项目中。

回答by Damb

You create custom initializer, which inherits from DropCreateDatabaseIfModelChangesor DropCreateDatabaseAlwaysinterface. Like:

您创建自定义初始化程序,它继承自DropCreateDatabaseIfModelChangesDropCreateDatabaseAlways接口。喜欢:

public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<-YourDbContext->

And then you overwrite Seed method like:

然后你覆盖 Seed 方法,如:

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.

编辑:设置完后,您还必须设置 Initializer,正如 Ladislav Mrnka 提到的那样。

Database.SetInitializer(new EntitiesContextInitializer());

ie.: in Global.asax:

即:在 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;.....

不要忘记添加using System.Data.Entity;......

回答by Ladislav Mrnka

You must create custom database initializer derived for example from DropCreateDatabaseIfModelChangesand fill data in overriden Seedmethod. Then you must use Database.SetInitializerto set your new initializer when application starts. Hereis example (from CTP5) used to create custom index in the database.

您必须创建自定义数据库初始值设定项,例如派生自DropCreateDatabaseIfModelChanges并在覆盖Seed方法中填充数据。然后,您必须Database.SetInitializer在应用程序启动时使用来设置新的初始化程序。是用于在数据库中创建自定义索引的示例(来自 CTP5)。

回答by tdykstra

For an example see the new MVC / Entity Framework tutorial series at http://www.asp.net/entity-framework/tutorials#Using%20MVCBoth #1 and #4 show initializer classes.

有关示例,请参阅http://www.asp.net/entity-framework/tutorials#Using%20MVC 上的新 MVC/实体框架教程系列 #1 和 #4 都显示初始化程序类。