One-to-One circular reference in EF

625 views Asked by At

I have a class (simplified) in code-first EF6.

public class Transaction
{
    public int Id { get; set; }        
    public int? TransferCounterPartId { get; set; }
    public Transaction TransferCounterPart { get; set; }
    public bool IsTransfer{get{return TransferCounterPart != null;}}
}

I would characterize this as a circular self reference. I have not added any fluent API, but have tried different scenario's to solve my issue.

My issue is on deleting these entities, or as it will, the couple that makes up a transfer-transaction.

My delete statement:

Manager.Context.Transactions.Remove(transaction.TransferCounterpart);
Manager.Context.Transactions.Remove(transaction);

The Exception:

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

If I set the references to eachother to null, SaveChanges, and then remove the entities and SaveChanges again, it goes without a hitch, but this has performance and design implications.

Questions: How can I delete these two entities?

Solution Directions I can image:

  • Model differently? (boundary condition, I must easily find the counterpart when I delete one transaction)
  • Context Settings or SaveChanges options EF?
  • Stored Procedures, or execute sql code ?
  • Fluent API?

Any help will be welcome

psuedo-code modelbuilder.Entity.HasOptional.HasRequired.WillCascasde(true/false) scenario's did not work.

0

There are 0 answers