【问题标题】:ASP.NET Core new project not creating database by defaultASP.NET Core 新项目默认不创建数据库
【发布时间】:2018-01-23 03:54:24
【问题描述】:

我使用标识 Individual User 创建了新的 ASP.NET Core 项目,但它没有创建任何数据库。

我必须手动使用add-migration update-database

我记得,在过去,一切都是自动完成的。不知道这次怎么了。

VS2017.3

【问题讨论】:

  • 分享您的代码以获得更好的帮助。@Wakka Sakka
  • 没有额外的代码。我刚刚创建了新的 aspnet core 1.1 项目,有一个迁移文件夹,但在服务器资源管理器中没有任何数据库。我必须手动update-database
  • ASP.NET Core 从未自动创建数据库。该模板始终提供脚手架迁移,但从未应用。当您发布您的应用程序时,它提供了在发布过程中应用迁移的选项。如果未创建数据库,则在运行应用程序时,第一次使用数据库访问时,它会给出错误屏幕,并带有应用迁移的选项。这一直是 ASP.NET Core 的行为。

标签: asp.net-mvc asp.net-core visual-studio-2017 entity-framework-core


【解决方案1】:

如果你只想创建一个数据库模式,你可以调用:

//IDbContext context;
context.Database.EnsureCreated();

But note, that

EnsureCreated 完全绕过迁移,只是为您创建架构,您不能将其与迁移混合。 EnsureCreated 专为测试或快速原型设计而设计,您可以每次都删除和重新创建数据库。如果您正在使用迁移并希望在应用启动时自动应用它们,那么您可以改用context.Database.Migrate()

【讨论】:

【解决方案2】:

您必须触发它才能自动迁移您的数据库。与以前版本的 ASP.NET MVC 不同,this isn't implemented out of the box.

然而,实现这一点的一种方法是从您的startup.cs 或管道早期的其他地方触发它,如下所示:

using (var context = new MyDbContext(..))
{
    context.Database.Migrate();
}

【讨论】:

    【解决方案3】:

    根据您拥有的 dotnet core 版本,ASP.NET Core 可能不会在您创建新应用时自动为您创建数据库。

    但是,只需迁移和更新数据库即可。 首先,使用语法dotnet ef migrations add <MIGRATION_NAME> 创建一个新的迁移。像这样

    dotnet ef migrations add InitialMigration

    然后你像这样更新数据库

    dotnet ef database update .

    这应该可以解决问题。

    【讨论】:

      【解决方案4】:

      要启用自动迁移,您需要将其添加到您的配置中:

      public class MigrateDBConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<DbContext>
      {
      public MigrateDBConfiguration()
      {
          AutomaticMigrationsEnabled = true;
          AutomaticMigrationDataLossAllowed = true;
      }
      }
      

      要启用对数据库的自动更新,您需要在上下文的 OnModelCreating() 方法中添加 Database.SetInitializer(...) :

      public class MyContext : DbContext
      {
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, MigrateDBConfiguration>());
      }
      
      ...
      }
      

      您还可以使用Fluent migration 使数据库自动更新和迁移。
      以下是来自Here 的关于如何使用它的示例:

      using FluentMigrator;
      namespace DatabaseMigration.Migrations
      {
          [Migration(1)]
          public class M0001_CreateMemberTable:Migration
          {
              public override void Up()
              {
                  Create.Table("Member")
                      .WithColumn("MemberId").AsInt32().PrimaryKey().Identity()
                      .WithColumn("Name").AsString(50)
                      .WithColumn("Address").AsString()
                      .WithColumn("MobileNo").AsString(10);
              }
      
              public override void Down()
              {
                  Delete.Table("Member");
              }
          }
      }
      

      【讨论】:

      • 我感觉这个答案是针对 Asp.NET MVC 而不是针对 ASP.NET Core?我认为 ASP.NET Core 没有“AutomaticMigrationsEnabled”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多