Note that there are some explanatory texts on larger screens.

plurals
  1. POCollection property not properly added to parent on save
    primarykey
    data
    text
    <p>Look at the following code example. </p> <p>What it does:</p> <ol> <li>Iterates a bunch of customers. If it already knows the customer, it retrieves the existing database object for that customer (this is the problem-ridden part). Otherwise, it creates a new object (this works fine).</li> <li>All loans where the social security number matches (CPR) will be added to the new or existing customer.</li> </ol> <p>The problem: it works for new customer objects, but when I retrieve an existing customer object, the loans lose their relation to the customer when saved (CustomerID = null). They are still saved to the database.</p> <p>Any ideas?</p> <pre><code>protected void BuildCustomerData() { Console.WriteLine(" Starting the customer build."); var counter = 0; var recycleCount = 100; var reportingCount = 100; var sTime = DateTime.Now; var q = from c in db.IntermediaryRkos select c.CPR; var distincts = q.Distinct().ToArray(); var numbersToProcess = distincts.Count(); Console.WriteLine(" Identified " + numbersToProcess + " customers. " + (DateTime.Now - sTime).TotalSeconds); foreach (var item in distincts) { var loans = from c in db.IntermediaryRkos where c.CPR == item select c; var existing = db.Customers.Where(x =&gt; x.CPR == item).FirstOrDefault(); if (existing != null) { this.GenerateLoanListFor(existing, loans); db.Entry(existing).State = System.Data.EntityState.Modified; } else { var customer = new Customer { CPR = item, }; this.GenerateLoanListFor(customer, loans); db.Customers.Add(customer); db.Entry(customer).State = System.Data.EntityState.Added; } counter++; if (counter % recycleCount == 0) { this.SaveAndRecycleContext(); } if (counter % reportingCount == 0) { Console.WriteLine(" Processed " + counter + " customers of " + numbersToProcess + "."); } } db.SaveChanges(); } protected void GenerateLoanListFor(Customer customer, IQueryable&lt;IntermediaryRko&gt; loans) { customer.Loans = new List&lt;Loan&gt;(); foreach (var item in loans.Where(x =&gt; x.DebtPrefix == "SomeCategory").ToList()) { var transformed = StudentLoanMap.CreateFrom(item); customer.Loans.Add(transformed); db.Entry(transformed).State = System.Data.EntityState.Added; } } </code></pre> <p><strong>EDIT 1:</strong></p> <p>As pointed out, I am manually setting the state. This is due to the RecycleContext call, which is implemented for maximum db transaction performance:</p> <pre><code>protected void SaveAndRecycleContext() { db.SaveChanges(); db.Dispose(); db = new SolutionDatabase(); db.Configuration.AutoDetectChangesEnabled = false; db.Configuration.ValidateOnSaveEnabled = false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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