Reputation: 3657
I have two entities, Customer and Email. I have stripped them to only show what is important.
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; }
}
I override the OnModelCreating() in my context class and it includes the following:
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");
});
This works fine and Creates FIVE tables in the DB, Customers, Emails, and the four M2M tables, CustomerAPEmails, CustomerInvoiceEmails, CustomerMainEmails, and CustomerOrderEmails.
Now, if I try to rename the Email entity to CustomerEmail and perform a migration, the first few lines in the migration are this:
RenameTable(name: "dbo.CustomerMainEmails", newName: "CustomerEmails");
DropForeignKey("dbo.CustomerMainEmails", "CustomerId", "dbo.Customers");
DropForeignKey("dbo.CustomerMainEmails", "Address", "dbo.Emails");
DropForeignKey("dbo.CustomerOrderEmails", "CustomerId", "dbo.Customers");
Doing an update-database then fails with this error:
System.Data.SqlClient.SqlException (0x80131904): Cannot find the object "dbo.CustomerMainEmails" because it does not exist or you do not have permissions.
I think the reason is the table is renamed, then it tries to drop keys on a table that doesn't exist. This seems to be in error to me. Is it a bug? Why does it rename the dbo.CustomerMainEmails table to begin with?
Upvotes: 3
Views: 1637
Reputation: 1970
Try to move the RenameTable()
method below
DropForeignKey("dbo.CustomerMainEmails", "CustomerId", "dbo.Customers");
DropForeignKey("dbo.CustomerMainEmails", "Address", "dbo.Emails");
DropForeignKey("dbo.CustomerOrderEmails", "CustomerId", "dbo.Customers");
RenameTable(name: "dbo.CustomerMainEmails", newName: "CustomerEmails");
Upvotes: 1