问题
Does it make sense to create new object before trying to save object that I already have loaded but with more then one Context?
After lots of tries and errors this code seems to work fine. Basically it's regetting values for Konsultanci
and Szkolenie
with the current Context based on objects that are read from ObjectListView.
var listaDoPodmiany = new List<SzkolenieUczestnicy>();
if (listaUczestnikow.GetItemCount() > 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 => p.KonsultantNazwa == currentUczestnik.Konsultanci.KonsultantNazwa),
Szkolenie = context.Szkolenies.First(p => 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);
}
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?
This is continuation (or more like conclusion from multiple questions I asked before):
- The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects
- Can context of Entity Framework be extracted from object later on?
- Can Entity Framework context be reused all the time in code?
回答1:
Does it make sense to create new object before trying to save object that I already have loaded but with more then one Context?
No. But it shows you have a design problem in your application. You shouldn't have to deal with multiple contexts for the same objects.
I think you have a problem with your contexts lifetime. This isn't an easy issue to solve, but here is an article that could help you. See also here.
My opinion is that contexts should have the shortest lifetime as possible. Long-running contexts are generally a bad practice, as you'll quickly encounter issues in your application (memory leak, problems when dealing with multi-threading or concurrency...etc.)
If you have a 3-tiers architecture, then you really should expose stateless services and create a new context per request. If you're accessing directly the database from your application (2-tiers application), for instance a Winform application that requests the DB directly, shorten the lifetime of your context as much as possible if you can't do stateless requests.
Final word: creating new contexts has a very small overhead, and it won't lead to performance issues 99% of the time.
来源:https://stackoverflow.com/questions/9093863/does-it-make-sense-to-create-new-object-in-entity-framework-before-saving-it-to