EF Code First错误地重命名表,这是一个bug吗?

3

我有两个实体,客户和电子邮件。我已将它们剥离出来,只展示了重要的部分。

public class Customer
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [StringLength(6)]
    public string Id { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    //Relationships
    public ICollection<Email> MainEmails { get; set; }
    public ICollection<Email> OrderEmails { get; set; }
    public ICollection<Email> InvoiceEmails { get; set; }
    public ICollection<Email> APEmails { get; set; }
}

public class Email
{
    [Key]
    [StringLength(50)]  
    public string Address { get; set; }

    public ICollection<Customer> MainCustomers { get; set; }
    public ICollection<Customer> OrderCustomers { get; set; }
    public ICollection<Customer> InvoiceCustomers { get; set; }
    public ICollection<Customer> APCustomers { get; set; }
}

我在我的上下文类中覆盖 OnModelCreating() 方法,以下是其内容:

modelBuilder.Entity<Customer>()
    .HasMany(e => e.MainEmails)
    .WithMany(c => c.MainCustomers)
    .Map(m =>
    {
        m.ToTable("CustomerMainEmails");
        m.MapLeftKey("CustomerId");
        m.MapRightKey("Address");
    });

modelBuilder.Entity<Customer>()
    .HasMany(e => e.OrderEmails)
    .WithMany(c => c.OrderCustomers)
    .Map(m =>
    {
        m.ToTable("CustomerOrderEmails");
        m.MapLeftKey("CustomerId");
        m.MapRightKey("Address");
    });

modelBuilder.Entity<Customer>()
    .HasMany(e => e.InvoiceEmails)
    .WithMany(c => c.InvoiceCustomers)
    .Map(m =>
    {
        m.ToTable("CustomerInvoiceEmails");
        m.MapLeftKey("CustomerId");
        m.MapRightKey("Address");
    });

modelBuilder.Entity<Customer>()
    .HasMany(e => e.APEmails)
    .WithMany(c => c.APCustomers)
    .Map(m =>
    {
        m.ToTable("CustomerAPEmails");
        m.MapLeftKey("CustomerId");
        m.MapRightKey("Address");
    });

这个工作很好,它在数据库中创建了五个表:客户(Customers),电子邮件(Emails),以及四个 M2M 表,CustomerAPEmails、CustomerInvoiceEmails、CustomerMainEmails 和 CustomerOrderEmails。

现在,如果我尝试将 Email 实体重命名为 CustomerEmail 并执行迁移,则迁移中的前几行代码是:

RenameTable(name: "dbo.CustomerMainEmails", newName: "CustomerEmails");
DropForeignKey("dbo.CustomerMainEmails", "CustomerId", "dbo.Customers");
DropForeignKey("dbo.CustomerMainEmails", "Address", "dbo.Emails");
DropForeignKey("dbo.CustomerOrderEmails", "CustomerId", "dbo.Customers");

执行update-database命令时,出现以下错误:

System.Data.SqlClient.SqlException (0x80131904):无法找到对象“dbo.CustomerMainEmails”,因为它不存在或您没有权限。

我认为原因是表名已更改,然后尝试在不存在的表上删除键。这对我来说似乎是错误的。这是一个错误吗?为什么要重命名dbo.CustomerMainEmails表?

1个回答

1
尝试将RenameTable()方法移动到下面。
DropForeignKey("dbo.CustomerMainEmails", "CustomerId", "dbo.Customers");
DropForeignKey("dbo.CustomerMainEmails", "Address", "dbo.Emails");
DropForeignKey("dbo.CustomerOrderEmails", "CustomerId", "dbo.Customers");
RenameTable(name: "dbo.CustomerMainEmails", newName: "CustomerEmails");

3
谢谢,但根本不应该重命名那个表格。 - BBauer42
1
就我个人而言,我也遇到了同样的问题,只不过我是创建了一个新表,然后迁移却要重命名一个完全无关的表。在执行 update-database 命令之前,我修改了迁移文件(我使用 Update-Database -Script 并运行生成的脚本)。虽然我不确定这种情况的条件是什么,但我想说的是,在更新之前一定要仔细检查迁移代码 :) - Paul Zaczkowski
1
@PaulZaczkowski 我也遇到了完全相同的问题。我创建了一个新表格,迁移自动生成了一行代码来重命名一个无关的表格。我不知道它为什么认为需要重命名这个表格。 - Justin Skiles
1
@JustinSkiles 继续我的问题,我尝试删除重命名行,这样我就不必修改依赖于它的存储过程,但是从那时起事情变得更加糟糕(耶)。我希望有时间深入了解一下,看看到底发生了什么。 - Paul Zaczkowski
1
@PaulZaczkowski 说句实话,我也遇到了同样的问题。我删除了添加的行,但 EF 仍然认为重命名已经发生,所有生成的查询都错误地引用了错误的重命名。 - Justin Skiles
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接