Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes it make sense to create new object in Entity Framework before saving it to resolve problems with multiple Contexts?
    primarykey
    data
    text
    <p>Does it make sense to create new object before trying to save object that I already have loaded but with more then one Context? </p> <p>After lots of tries and errors this code seems to work fine. Basically it's regetting values for <code>Konsultanci</code> and <code>Szkolenie</code> with the current Context based on objects that are read from ObjectListView. </p> <pre><code> var listaDoPodmiany = new List&lt;SzkolenieUczestnicy&gt;(); if (listaUczestnikow.GetItemCount() &gt; 0) { foreach (SzkolenieUczestnicy currentUczestnik in listaUczestnikow.Objects) { using (var context = new EntityBazaCRM(Settings.sqlDataConnectionDetailsCRM)) { if (currentUczestnik.SzkolenieUczestnicyID == 0) { var nowy = new SzkolenieUczestnicy { Konsultanci = context.Konsultancis.First(p =&gt; p.KonsultantNazwa == currentUczestnik.Konsultanci.KonsultantNazwa), Szkolenie = context.Szkolenies.First(p =&gt; p.SzkolenieID == varSzkolenie.SzkolenieID), SzkolenieUzytkownik = currentUczestnik.SzkolenieUzytkownik, SzkolenieUzytkownikData = currentUczestnik.SzkolenieUzytkownikData, UczestnikPotwierdzilUdzial = currentUczestnik.UczestnikPotwierdzilUdzial, UczestnikPrzybyl = currentUczestnik.UczestnikPrzybyl }; context.SzkolenieUczestnicies.AddObject(nowy); context.SaveChanges(); listaDoPodmiany.Add(nowy); } else { context.SzkolenieUczestnicies.Attach(currentUczestnik); context.ObjectStateManager.ChangeObjectState(currentUczestnik, EntityState.Modified); context.SaveChanges(); listaDoPodmiany.Add(currentUczestnik); } } } listaUczestnikow.ClearObjects(); listaUczestnikow.SetObjects(listaDoPodmiany); } </code></pre> <p>This seems to solve lots of problems I was getting about multiple contexts. But is it good approach? Will there be much impact on speed since I guess Konsultanci and Szkolenie will be read another time from SQL?</p> <p>This is continuation (or more like conclusion from multiple questions I asked before):</p> <ul> <li><a href="https://stackoverflow.com/questions/9083709/the-relationship-between-the-two-objects-cannot-be-defined-because-they-are-atta">The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects</a> </li> <li><a href="https://stackoverflow.com/questions/9087427/can-context-of-entity-framework-be-extracted-from-object-later-on">Can context of Entity Framework be extracted from object later on?</a> </li> <li><a href="https://stackoverflow.com/questions/9086722/can-entity-framework-context-be-reused-all-the-time-in-code">Can Entity Framework context be reused all the time in code?</a></li> </ul>
    singulars
    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.
 

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