I have boiled this down to a pretty minimal use case:
public class ItemRental
{
[Key]
public Int32 ItemRentalId { get; set; }
public Int32? OriginatingSalesOrderId { get; set; }
[ForeignKey("OriginatingSalesOrderId")]
public SalesOrder OriginatingSalesOrder { get; set; }
public Int32? DepositCreditedOnSalesOrderId { get; set; }
[ForeignKey("DepositCreditedOnSalesOrderId")]
public SalesOrder DepositCreditedOnSalesOrder { get; set; }
}
public class SalesOrder
{
[Key]
public Int32 SalesOrderId { get; set; }
[InverseProperty("OriginatingSalesOrder")]
public ICollection<ItemRental> Rentals { get; set; }
[InverseProperty("DepositCreditedOnSalesOrder")]
public ICollection<ItemRental> Refunds { get; set; }
}
public class MyAppDatabase : DbContext
{
public MyAppDatabase(DbContextOptions<MyAppDatabase> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(x => x.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
base.OnModelCreating(modelBuilder);
}
public DbSet<ItemRental> ItemRentals { get; set; }
public DbSet<SalesOrder> SalesOrders { get; set; }
}
Trying to run the migration will give:
System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'ItemRental.OriginatingSalesOrder' of type 'SalesOrder'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
This same relationship is perfectly fine with EF 6.x. I am sure I could solve this using the Fluent API, but I would rather understand how to make this work using annotations.
I found a similar question here: EntityFramework core model relationship issue while doing Add-Migration But it doesn't solve this problem.
EDIT: Sample solution here: https://drive.google.com/file/d/0BzgvtZfXt8MHd1RseVJubmd6TEU/view?usp=sharing