ASP.NET Core RC2 种子数据库 [英] ASP.NET Core RC2 Seed Database

查看:13
本文介绍了ASP.NET Core RC2 种子数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我试图用数据为 Entity Framework Core 数据库做种子,在我看来,下面的代码显示了工作.我意识到这不应该在 ApplicationDbContext 构造函数中调用,而应该从 startup 调用,但我不知道如何做到这一点.

My problem is i am trying to seed an Entity Framework Core database with data and in my mind the below code show work. I've realised that this should not be called in the ApplicationDbContext constructor and should be called from the startup but im not sure how to do this.

基于Ketrex提供的解决方案,我的解决方案如下:

Based on the solution provided by Ketrex, my solution is as follows:

Startup.cs:

Startup.cs:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        ... 

        app.ApplicationServices.GetRequiredService<ApplicationDbContext>().Seed();
    }

种子扩展:

public static class DbContextExtensions
{
    public static void Seed(this ApplicationDbContext context)
    {
        // Perform database delete and create
        context.Database.EnsureDeleted();
        context.Database.EnsureCreated();

        // Perform seed operations
        AddCountries(context);
        AddAreas(context);
        AddGrades(context);
        AddCrags(context);
        AddClimbs(context);

        // Save changes and release resources
        context.SaveChanges();
        context.Dispose();
    }

    private static void AddCountries(ApplicationDbContext context)
    {
        context.AddRange(
            new Country { Name = "England", Code = "En" },
            new Country { Name = "France", Code = "Fr" }
            );
    }

    ...
}

我知道在 Entity Framework 的优先级列表中播种数据库是相当高的,但是如果有一些关于如何实现这个微不足道的任务或至少提供临时解决方法的文档,那就太好了.如果有人可以提供有关如何执行此操作的指导,将不胜感激.我觉得我已经接近解决方案了,但无法拼凑起来.

I understand that seeding a database is quite high up on the priority list for Entity Framework but it would be great if there was some documentation on how to achieve this trivial task or at least provide a temporary work around. If someone can provide some guidance on how to do this it would be greatly appreciated. I feel i'm close to a solution but just cant piece it together.

感谢您的帮助.

推荐答案

假设您使用的是内置 DI 容器,这里有一种方法可以实现这一点.

Assuming you are using the built-in DI container, here is one way you can accomplish this.

在启动类的 Configure 方法中引用您的种子方法,并将 IApplicationBuilder 对象作为参数而不是 DbContext 传递,如下所示:

Reference your seed method in the Configure method of your startup class, and pass the IApplicationBuilder object as a parameter instead of the DbContext, like this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    //...
    // Put this at the end of your configure method
    DbContextSeedData.Seed(app);
}

接下来,修改您的种子方法以接受 IApplicationBuilder 实例.然后您将能够启动 DbContext 的一个实例,并执行您的种子操作,如下所示:

Next, modify your seed method to accept the IApplicationBuilder instance. Then you'll be able to spin up an instance of the DbContext, and perform your seed operation, like this:

public static void Seed(IApplicationBuilder app)
{
    // Get an instance of the DbContext from the DI container
    using (var context = app.ApplicationServices.GetRequiredService<ApplicationDbContext>())
    {
        // perform database delete
        context.Database.EnsureDeleted;
        //... perform other seed operations
    }
}

这篇关于ASP.NET Core RC2 种子数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆