在 Entity Framework Core 中自动创建数据库 [英] auto create database in Entity Framework Core

查看:44
本文介绍了在 Entity Framework Core 中自动创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在移植到 .NET Core 的应用程序将使用带有 SQLite 的新 EF Core.我想在应用程序首次运行时自动创建数据库和表结构.根据 EF 核心文档,这是使用手动命令完成的

My application which is being ported to .NET core will use the new EF Core with SQLite. I want to automatically create the database and table structures when the app is first run. According to the EF core documentation this is done using manual commands

dotnet ef migrations add MyFirstMigration

dotnet ef database update

但是,我不希望最终用户输入这些命令,而是希望应用程序创建并设置数据库以供首次使用.对于 EF 6,有类似

However I don't want the end user to enter these commands and would prefer to have the app create and setup the database for first use. For EF 6 there is functionality like

Database.SetInitializer(new CreateDatabaseIfNotExists<MyContext>());

但在 EF Core 中,这些似乎不存在.我找不到与 EF 核心等效的任何示例或文档,并且在 EF 核心文档的缺失功能列表中没有提到它.我已经设置了模型类,因此我可以编写一些代码来基于模型初始化数据库,但如果框架自动执行此操作会更容易.我不想自动构建模型或迁移,只想在新数据库上创建表结构.

But in EF Core these don't seem to exist. I can't find any examples or documentation on something equivalent for EF core and it is not mentioned in the list of missing features in the EF core documentation. I have the model classes setup already so I could write some code to initialize the database based on the models but it would be heaps easier if the framework did this automatically. I don't want to auto build the model or migrate, just create the table structures on a new database.

我在这里遗漏了什么还是 EF 核心中缺少自动创建表功能?

Am I missing something here or is auto create table function missing in EF core?

推荐答案

如果您已经创建了迁移,您可以在 Startup.cs 中执行它们,如下所示.

If you have created the migrations, you could execute them in the Startup.cs as follows.

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
 {
      using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
      {
            var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
            context.Database.Migrate();
      }
      
      ...

这将使用您添加的迁移创建数据库和表.

This will create the database and the tables using your added migrations.

如果您不使用实体框架迁移,而只需要在首次运行时完全按照上下文类中的方式创建 DbContext 模型,那么您可以使用:

If you're not using Entity Framework Migrations, and instead just need your DbContext model created exactly as it is in your context class at first run, then you can use:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
 {
      using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
      {
            var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
            context.Database.EnsureCreated();
      }
      
      ...

相反.

如果您需要在确保创建数据库之前删除它,请调用:

If you need to delete your database prior to making sure it's created, call:

            context.Database.EnsureDeleted();

就在您调用 EnsureCreated()

Just before you call EnsureCreated()

改编自:http://docs.identityserver.io/en/latest/quickstarts/7_entity_framework.html?highlight=entity

这篇关于在 Entity Framework Core 中自动创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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