【问题标题】:Entity Framework 7 migration not creating tablesEntity Framework 7 迁移不创建表
【发布时间】:2016-04-12 02:13:45
【问题描述】:

我正在使用 Entity Framework 7 处理我的第一个项目,并正在连接到已创建数据库但其中还没有表的 SQL Server。我创建了我的DbContext 并创建了一个类,然后在我的上下文中设置了一个DbSet<>。我运行命令来启用迁移并创建第一个迁移,然后运行命令来更新数据库。一切看起来工作正常,没有出现错误,但是当我查看数据库时,只创建了 EFMigraitonsHistory 表。当我查看为初始迁移创建的类时,它基本上是空白的。我做错了什么?

我正在运行的命令:

dnvm install latest -r coreclr
dnx ef migrations add MyFirstMigration
dnx ef database update

上下文:

namespace JobSight.DAL
{
    public class JobSightDBContext : DbContext
    {
        public DbSet<NavigationMenu> NavigationMenu { get; set; }
    }
}

表类:

namespace JobSight.DAL
{
    public class NavigationMenu
    {
        [Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Int16 ID { get; set; }

        public string ControllerName { get; set; }
        public string ActionName { get; set; }
        public string ExternalURL { get; set; }
        public string Title { get; set; }
        public Int16? ParentID { get; set; }

        public virtual NavigationMenu Parent { get; set; }
    }
}

Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<JobSightDBContext>(options =>
                {
                    options.UseSqlServer(Configuration["Data:JobSightDatabase:ConnectionString"]);
                });
    }

初始迁移类(由 EF 自动生成):

namespace JobSight.WebUI.Migrations
{
    public partial class Initial : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
        }
    }
}

编辑: 在按照 Poke 的建议进行操作之后,这是我新的自动生成的迁移。不过,该表仍未在数据库级别创建。

namespace JobSight.WebUI.Migrations
{
    public partial class MyFirstMigration : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "NavigationMenu",
                columns: table => new
                {
                    ID = table.Column<short>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    ActionName = table.Column<string>(nullable: true),
                    ControllerName = table.Column<string>(nullable: true),
                    ExternalURL = table.Column<string>(nullable: true),
                    ParentID = table.Column<short>(nullable: true),
                    Title = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_NavigationMenu", x => x.ID);
                    table.ForeignKey(
                        name: "FK_NavigationMenu_NavigationMenu_ParentID",
                        column: x => x.ParentID,
                        principalTable: "NavigationMenu",
                        principalColumn: "ID",
                        onDelete: ReferentialAction.Restrict);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable("NavigationMenu");
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    您需要先在数据库上下文中设置实体。至少,您需要这样做:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    
        modelBuilder.Entity<NavigationMenu>();
    }
    

    您的迁移问题有点隐藏在您的项目布局中。因此,您拥有的是一个包含实体和数据库上下文的 JobSight.DAL 项目。然后你有一个项目JobSight.WebUI,它是包含Startup.cs 和数据库设置的实际ASP 项目。

    这会导致问题,因为默认情况下 EF 只会假定在当前程序集中找到所有内容。因此,如果您从 Web 项目启动 ef 命令,即使上下文在另一个项目中,它也会在其中创建迁移。但是当您尝试应用迁移时,EF 将找不到它,因为它只会在上下文的项目中查找。

    因此,要解决此问题,您需要在 DAL 项目中创建迁移。您可以通过在调用ef 命令时指定项目来做到这一点:

    dnx ef migrations add Example -p JobSight.DAL
    

    之后您可以通过运行dnx ef migrations list 来验证这是否有效。现在应该返回 Example 迁移;以前,该命令没有返回任何内容:它找不到迁移,这就是为什么update 命令只说Done(没有应用迁移)并且没有创建数据库的原因。因此,如果您现在在那里进行迁移,则可以使用以下方法应用它:

    dnx ef database update
    

    请注意,由于现在在 DAL 项目中创建了迁移,因此您需要在此处添加对 EntityFramework.MicrosoftSqlServer 的引用,否则项目将无法编译。在运行上面的list 命令之前,您需要这样做。

    最后,有关这方面的更多信息,请参阅this issue

    【讨论】:

    • 我将覆盖代码添加到我的上下文中,现在迁移类中确实包含生成表的代码,但是在运行更新命令时它仍然没有构建我的表。我已经用我正在运行的命令更新了原始帖子。
    • ef database update 是否返回任何输出?您确定您正在查看正确的数据库吗?检查您的连接字符串以确保您使用的是什么。
    • 我确信它是正确的数据库。我可以删除 EFMigrationHistory 表并重新创建它,而且它是服务器上唯一的数据库。当我运行dnx ef database update 命令时,它所做的只是坐在那里然后说done。由于数据库已经存在但没有任何表,我是否需要做一些特别的事情?
    • 您确实创建了一个新迁移,并且该迁移在 Up 部分中有一个 CreateTable 调用,对吧?
    • 为我生成了一个新的迁移类,它确实包含CreateTable 命令。我稍后会在 OP 中发布新的
    【解决方案2】:

    虽然这不是原始问题的答案,但我在这里发布我的答案,因为它可能会帮助遇到类似问题的人。我的问题还在于没有创建表,但dotnet ef migrations add InitialCreate 确实在 Migrations 文件夹中创建了 3 个 .cs 文件。但是dotnet ef database update 只创建了 MigrationsHistory 表,dotnet ef migrations list 没有返回任何迁移。

    原来问题在于 Migrations 文件夹已从 Visual Studio 项目中排除。一旦我再次包含它,一切正常。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我就是这样解决的

      1. 我在 SQL 中删除了我的数据库
      2. 我更改了用于上一次迁移的名称。我从“Add-Migration InitialCreate”更改为“Add-Migration NewName”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-07
        • 1970-01-01
        • 2019-02-12
        • 2016-05-08
        • 2015-08-15
        • 1970-01-01
        • 2023-03-03
        • 2014-02-07
        相关资源
        最近更新 更多