estoy intentando hacer una relación muy simple Una-A-Muchos con propiedad de navegación y listado de entidades hijas. Si lo hago por defecto parece que todo lo hace bien, pero si intentado cambiar el comportamiento de la FK al borrar ponerlo a Null, me duplica la FK creándome una con el OnDelete SetNull y la otra por defecto
Entity Framework 2.2
Entidades
public class BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
}
public class Invoice: BaseEntity
{
[Display(Name = "Invoice Number")]
public string Number { get; set; }
[Display(Name = "Amount")]
[DataType(DataType.Currency)]
public decimal Amount { get; set; }
public ICollection<InvoiceLine> InvoiceLine { get; set; }
}
public class InvoiceLine: BaseEntity
{
[Display(Name = "Invoice")]
[ForeignKey("Invoice")]
public int? InvoiceID { get; set; }
public Invoice Invoice { get; set; }
public string ItemName { get; set; }
}
[...]
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Invoice>().ToTable("Invoice");
modelBuilder.Entity<InvoiceLine>().ToTable("InvoiceLines");
}
Parte donde modifico el ModelBuilder para cambiar a SetNull on Delete
modelBuilder.Entity<Invoice>().ToTable("Invoice");
modelBuilder.Entity<Invoice>().HasMany(i => i.InvoiceLine).WithOne("Invoice").OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<InvoiceLine>().ToTable("InvoiceLines");
modelBuilder.Entity<InvoiceLine>().HasOne(i => i.Invoice).WithMany().OnDelete(DeleteBehavior.SetNull);
He intentado poner solo una parte de la defición de la FK, ya sea desde Invoice o desde InvoiceLine (HasOne o HasMany) con el mismo resultado. Este es la salida del Add-Migration con la FK DUPLICADA
migrationBuilder.CreateTable(
name: "InvoiceLines",
columns: table => new
{
ID = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Visible = table.Column<bool>(nullable: false),
InvoiceID = table.Column<int>(nullable: true),
ItemName = table.Column<string>(nullable: true),
InvoiceID1 = table.Column<int>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_InvoiceLines", x => x.ID);
table.ForeignKey(
name: "FK_InvoiceLines_Invoice_InvoiceID",
column: x => x.InvoiceID,
principalTable: "Invoice",
principalColumn: "ID",
onDelete: ReferentialAction.SetNull);
table.ForeignKey(
name: "FK_InvoiceLines_Invoice_InvoiceID1",
column: x => x.InvoiceID1,
principalTable: "Invoice",
principalColumn: "ID",
onDelete: ReferentialAction.Restrict);
});
Según la doc de microsoft, creo que lo stoy haciendo bien, por qué me genera otra FK?
Gracias