【问题标题】:How to create EF migration classes如何创建 EF 迁移类
【发布时间】:2020-02-27 07:01:30
【问题描述】:

我似乎很难找到实施 EF Core 所需的信息。看了很多文章,看了Getting Started with Entity Framework Core [1 of 5]等视频。但这些文章和视频都没有假定使用最新版本的 Visual Studio (16.3.6) 创建的 Razor Pages 项目。

我更喜欢(并且已经熟悉)数据库优先,但微软已经决定我们需要先成为代码。

所以我手动创建了我的实体类并运行了Add-Migration 命令。 (注意 00000000000000_CreateIdentitySchema.cs 是我在创建项目时由 Visual Studio 创建的。)

它运行成功但创建了一个空的Migration 类。

public partial class InitialTaskClasses : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {

    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {

    }
}

无论如何让它将我的实体类合并到迁移中?

*请注意,我已经完成了一个update-database 命令,它将实体类添加到我的数据库中。

【问题讨论】:

  • @Herohtar: ApplicationDbContext,派生自IdentityDbContext,有一个空的构造函数,但没有其他方法或属性。
  • 那是你的问题。迁移是根据 DbContext 中定义的内容创建的。如果那是空的,那么迁移也将是。
  • 请在您的 dbcontext 文件中添加代码。
  • @Herohtar:嗯,那太好了。但我的问题是如何让它工作。我不知道我应该在那里输入什么。迁移似乎也适用于实体类,它们也不会出现在 DBContext 类中。
  • @nurdyguy:你能说得更具体点吗?哪些文件?

标签: c# asp.net entity-framework-core razor-pages


【解决方案1】:
public DbSet<Area> Area{ get; set; }
public DbSet<Goal> Goal{ get; set; }
public DbSet<Task> Task{ get; set; }

这应该作为 ApplicationDbContext 中的属性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多