Note that there are some explanatory texts on larger screens.

plurals
  1. POCode First DB deadlock
    text
    copied!<p>So, I am using Code First approach to building an application and ran into a little problem while saving an entity in the db.</p> <p>Here is my entity:</p> <pre><code>public class Customer { public Customer() { Addresses = new List&lt;Address&gt;(); PhoneNumbers = new List&lt;Phone&gt;(); } public Guid CustomerID { get; set;} public virtual ICollection&lt;Address&gt; Addresses { get; set;} public virtual ICollection&lt;Phone&gt; PhoneNumbers { get; set;} public virtual Address MailingAddress {get; set;} public virtual Phone DefaultPhone {get; set;} } </code></pre> <p>Now, this creates an entry in the Address and Phone table called Customer_CustomerID, and an entry in the Customer table of DefaultPhone_PhoneID and MailingAddress_AddressID, both Guids.</p> <p>Whenever I save my entity, I receive the following message:</p> <pre><code>"Inner Exception: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.” </code></pre> <p>I have a feeling it has to do with the way these entities are being hooked up, do you notice anything wrong or something I'm missing? </p> <p>This is how the entity is saved:</p> <pre><code>public ActionResult CreateCustomer(Address address, Phone phone) { Customer cust = new Customer(); repository.Phone_Save(phone); cust.PhoneNumbers.add(phone); cust.DefaulPhone = phone; repository.Address_Save(address); cust.Addresses.add(address); cust.MailingAddress = address; repository.Customer_Save(cust); } public void Customer_Save(Customer customer); { Customer entity = context.Customers.Find(customer.CustomerID); if (entity == null) { context.Customers.Add(customer); entity = customer; } else { context.Entry(entity).CurrentValues.SetValues(customer); } context.SaveChanges(); context.Entry(entity).Reload();} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload