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

查看:79
本文介绍了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方法中引用您的seed方法,并将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天全站免登陆