繁体   English   中英

EF-Core:表“名称”已存在 - 尝试更新数据库时

[英]EF-Core: Table "name" already exists - when trying to update database

ASP 核心 3.1 - API。 我正在使用最新版本的 Entity Framework Core。

我创建了一个表ToDoItem和一个ToDoItemContext 创建初始迁移并运行update-database 我现在在我的数据库中有那个表。 我现在添加了一个名为: ToDoItemDescription的新模型。

创建新迁移后尝试更新数据库时,出现错误:

表 'todoitems' 已经存在

更多细节:我有两个上下文,这是我运行的命令:

update-database -context todoitemscontext

我也试过:

update-database -context todoitemscontext -migration AddDescription

这是我的完整代码:

楷模:

public class TodoItem : IEntity 
{
    public long Id { get; set; }
    public string Name { get; set; }
    bool IsComplete { get; set; }
}

public class ToDoItemDescription 
{
    public int id { get; set; }
    public string Description { get; set; }
    //public int ToDoItemId { get; set; }
    public TodoItem TodoItem { get; set; }
}

语境:

public class TodoItemsContext : DbContext 
{
   public TodoItemsContext(DbContextOptions<TodoItemsContext> options) : base(options) { }

   public DbSet<TodoItem> TodoItems { get; set; }
   public DbSet<ToDoItemDescription> TodoItemsDescription { get; set; }
}

迁移:

[DbContext(typeof(TodoItemsContext))]
partial class TodoItemsContextModelSnapshot : ModelSnapshot 
{
    protected override void BuildModel(ModelBuilder modelBuilder) {
    #pragma warning disable 612, 618
    modelBuilder
        .HasAnnotation("ProductVersion", "3.1.9")
        .HasAnnotation("Relational:MaxIdentifierLength", 64);
    modelBuilder.Entity("project.Models.ToDoItemDescription", b => {
        b.Property<int>("id")
        .ValueGeneratedOnAdd()
        .HasColumnType("int");
        b.Property<string>("Description")
        .HasColumnType("longtext CHARACTER SET utf8mb4");
        b.Property<long?>("TodoItemId")
        .HasColumnType("bigint");
        b.HasKey("id");
        b.HasIndex("TodoItemId");
        b.ToTable("TodoItemsDescription");
    });

    modelBuilder.Entity("project.Models.TodoItem", b => {
        b.Property<long>("Id")
        .ValueGeneratedOnAdd()
        .HasColumnType("bigint");
        b.Property<bool>("IsComplete")
        .HasColumnType("tinyint(1)");
        b.Property<string>("Name")
        .HasColumnType("longtext CHARACTER SET utf8mb4");
        b.HasKey("Id");
        b.ToTable("TodoItems");
    });
    modelBuilder.Entity("project.Models.ToDoItemDescription", b =>
    {
    b.HasOne("project.Models.TodoItem", "TodoItem")
        .WithMany()
        .HasForeignKey("TodoItemId");
    });
#pragma warning restore 612, 618
}

public partial class TodoItems_Initial : Migration
{
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "TodoItems",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(nullable: true),
                    IsComplete = table.Column<bool>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TodoItems", x => x.Id);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "TodoItems");
        }
    }

public partial class AddDescription : Migration
{
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "TodoItemsDescription",
                columns: table => new
                {
                    id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Description = table.Column<string>(nullable: true),
                    TodoItemId = table.Column<long>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TodoItemsDescription", x => x.id);
                    table.ForeignKey(
                        name: "FK_TodoItemsDescription_TodoItems_TodoItemId",
                        column: x => x.TodoItemId,
                        principalTable: "TodoItems",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_TodoItemsDescription_TodoItemId",
                table: "TodoItemsDescription",
                column: "TodoItemId");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "TodoItemsDescription");
        }
}

谢谢你。

如果您在没有迁移的情况下预先创建了数据库,例如使用DbContext.Database.EnsureCreated(); .

这通常发生在您有一个创建表的迁移并且所需的表已经存在于您的数据库中时,因此,当您从 Migration 中的类更新数据库时,它将尝试创建一个表并且会失败,因为 Create 命令不会执行,因为它已经具有该特定表。

因此,为了避免该错误,您可能希望删除迁移类或对该类的Up()方法中的代码进行注释,以便它不执行该特定的创建命令。

它可以帮助人们在 Linux 和 Windows 上使用 MySQL 数据库

TL; 博士;

  • 我必须将表 __efmigrationshistory(注意小写)重命名为 __EFMigrationsHistory(注意大小写),因此命令行dotnet-ef database update设法验证表__EFMigrationsHistory上存在的所有迁移,因此,在表,说租户

更多的

  • 我必须在 Linux、Windows、MacOs 机器上工作。 主要使用 Visual Studio 代码和 .net core 3.1.xxx
  • 我使用代码优先的方法。 MySQL 数据库首先是在 Windows 机器上创建的,所有的表都是小写的
  • 切换到 Linux 机器后,我意识到案例很重要,因此,例如,手动将表“租户”重命名为“租户”。
  • 一旦我不得不在租户的 c# 类上创建一个新字段,我运行: dotnet-ef migrations add new-ftpSettings-fielddotnet-ef database update ,我得到表“Order”已经存在。 注意我试图在“租户”表中插入一个新字段
  • 经过大量的调查和搜索,我决定再次刷新数据库,我看到了“两个可疑表” __efmigrationshistory__EFMigrationsHistory
  • 我将空表__EFMigrationsHistory重命名为 like Table1 (作为备份),因此将表__efmigrationshistory重命名为__EFMigrationsHistory
  • 我运行了dotnet-ef database update ,该字段已正确添加到 MySQL 数据库中。

*** 就像您可能已经想到的那样,在 Linux 上运行命令行dotnet-ef database update正在创建一个新的(和)空表__EFMigrationsHistory到 MySQL 数据库,而它已经在__efmigrationshistory创建了一个小写的表(好的一个,在我的 Windows 机器上创建,包含所有迁移)。

*** 这是我的第一个贡献。 欢迎任何建议!

注意安全! Tchau/Au revoir!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM