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

发布于 2024-11-01 07:36:53 字数 193 浏览 1 评论 0原文

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

应用程序的结构如下:

Repository>服务> WebMVC

xml 位于 WebMVC 项目中。

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

The xml is in the WebMVC project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北凤男飞 2024-11-08 07:36:53

您创建自定义初始值设定项,它继承自 DropCreateDatabaseIfModelChangesDropCreateDatabaseAlways 接口。像:

public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<-YourDbContext->

然后你覆盖种子方法,像:

protected override void Seed(YourDbContext context)

整个例子可能看起来像:

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();

    }
}

编辑: 设置完之后,你也必须设置初始化程序,正如 Ladislav Mrnka 提到的。

Database.SetInitializer(new EntitiesContextInitializer());

即:在 Global.asax 中:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    Database.SetInitializer(new EntitiesContextInitializer());
}

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

You create custom initializer, which inherits from DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways interface. Like:

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.

Database.SetInitializer(new EntitiesContextInitializer());

ie.: in 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;
.....

你げ笑在眉眼 2024-11-08 07:36:53

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

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.

丑疤怪 2024-11-08 07:36:53

有关示例,请参阅新的 MVC / Entity Framework 教程系列:
http://www.asp.net/entity-framework/tutorials#Using%20MVC
#1 和 #4 都显示初始值设定项类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文