EntityFrameworkCore:如何初始化数据库并在用户首次使用应用程序时播种 [英] EntityFrameworkCore: How to initialize a Database and seed it the first time user uses an application

查看:537
本文介绍了EntityFrameworkCore:如何初始化数据库并在用户首次使用应用程序时播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Microsoft Visual Studio 2015和EntityFrameworkCore构建了一个项目。

I have build a project using Microsoft Visual Studio 2015 and EntityFrameworkCore.

我手动为几个虚拟数据创建了种子,并且正在开发解决方案。现在,我想在服务器中部署,但遇到的问题是,第一次启动应用程序时,由于找不到数据库和数据,它崩溃了。

I have seed manually a couple of dummy data and I was developing my solution. Now, I want to deploy the in the server, but I get the problem that by starting the application the first time, it crash since it does not find a data base and data.

我已经搜索了一下,并使用需要该软件包的 CreateDatabaseIfNotExists 类找到了Visual Studio 2013及更早版本的解决方案: System.Data.Entity
http://www.entityframeworktutorial.net/ code-first / database-initialization-strategy-in-code-first.aspx ),但是,此类类和包在EntityFrameworkCore中不存在。

I have googled and I find the solution for Visual Studio 2013 and previous using the CreateDatabaseIfNotExists class that need the package: System.Data.Entity (http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx), however, such classes and packages do not exist in EntityFrameworkCore.

如果用户第一次在EntityFrameworkCore中使用我的应用程序,如何创建并填充至少一行的数据库?

How does I create and populate a database with at least one row if user is using my application by the first time in EntityFrameworkCore?

或等效于实体框架核心中的System.Data.Entity

推荐答案

Rowan Miller 表示 ApplyMigrations 足以创建数据库(如果不存在)并应用所有(必要的)迁移。

Rowan Miller says that ApplyMigrations is enough to create database (if not exist) and apply all (nesessary) migrations.

创建方法如下:

public void CreateDbAndSampleData(IServiceProvider applicationServices)
{
    using (var serviceScope = applicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
    {
        using (var db = serviceProvider.GetService<ApplicationDbContext>()) 
        {
            // This will [try to] create database
            // and apply all necessary migrations
            db.Database.AsRelational().ApplyMigrations();

            // then you can check for existing data and modify something
            var admin = db.Users.Where(x => x.Name == "Superadmin").FirstOrDefault();
            if (admin == null)
            {
                db.Users.Add(new User {...});
                db.SaveChanges();
            }
        }
    }
}

Startup.cs 调用它,在 Configure 方法的结尾:

And call it from your Startup.cs, at end of Configure method:

CreateDbAndSampleData(app.ApplicationServices);

此代码将在每次应用启动时运行,因此您需要准确且不要覆盖任何非关键数据更改(例如更改用户的评论等)

This code will run on every app startup, so you need to be accurate and do not overwrite any non-critical data changes (like changing Users's comment etc)

您可以使用 MusicStore 应用作为示例: Startup.cs SampleData.cs

You can use MusicStore app as a sample: Startup.cs and SampleData.cs

这篇关于EntityFrameworkCore:如何初始化数据库并在用户首次使用应用程序时播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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