Confuse tracking in EF (updating an object with a child collection)

So, I'm new to EF (I use EF6), and I have a problem understanding the concept, I'm trying to update an object using a child collection.

Here is my entity class:

public class TimeSheet { public int TimeSheetID { get; set; } public virtual ICollection<TimeSheetDetail> Details { get; set; } } public class TimeSheetDetail { public int TimeSheetDetailID { get; set; } public int TimeSheetID { get; set; } public virtual TimeSheet TimeSheet { get; set; } } 

My update method:

 public void Update(TimeSheet obj) { var objFromDB = Get(obj.TimeSheetID); var deletedDetails = objFromDB.Details.Except(obj.Details).ToList(); _dbContext.Entry(obj).State = EntityState.Modified; //track if details exist foreach (var details in obj.Details) { _dbContext.Entry(details).State = details.TimeSheetDetailID == 0 ? EntityState.Added : EntityState.Modified; } //track deleted item foreach (var deleted in deletedDetails) { _dbContext.Entry(deleted).State = EntityState.Deleted; } } public TimeSheet Get(object id) { //return _timeSheet.Find(id); //Without AsNoTracking I got error int x = Convert.ToInt32(id); return _timeSheet.AsNoTracking().SingleOrDefault(a => a.TimeSheetID == x); } 

Above the code, give me Attaching an entity of type 'ClassName' failed because another entity of the same type already has the same primary key value . So my question is:

  • How do you update a child collection using EF? This means that I need to add a new one if it does not exist in the database, update it otherwise, or remove it from the database if it is deleted in POST .

  • If I do not use AsNoTracking() , it will throw Saving or accepting changes failed because more than one entity of type 'ClassName' have the same primary key value . I noticed that the error was due to the fact that my DbSet added data from the database to the Local property, unless I use AsNoTracking() , which causes the EF structure to throw an error because it thinks I have duplicate data. How does it really work?

  • As you can see, I am trying to compare objFromDb with obj to check if the user is deleting one of the details so that I can delete it from the database. Instead, I got a bunch of DynamicProxies from the result of the collection. What is DynamicProxies and how does it work?

  • Is there a good article or 101 tutorial on EF? So far I see only a simple one that does not help my business, and I look around and find a mixed answer on how to do things. Honestly, at the moment I would just like to go with the classic ADO.Net instead of EF.

+3
c # entity-framework entity-framework-6
source share
1 answer

To better understand the structure of an entity, think of DbContext as a proxy between your application and the database. DbContext will cache everything and will use every bit of data from the cached values ​​if you do not tell it about it.

For 1 .. It depends on your environment, if your DbContext not located between selecting and updating entites, you can simply call SaveChanges and your data will be saved. If your DbContext configured, you can detach the entites from the context, modify the data, reconnect it, and set the EntityState to change.

I cannot give you a 100% answer because I stopped using the entity structure about six months ago. But I know that it’s painful to update complex relationships.

For 2 .: the AsNoTracking team tells EF not to track changes made to entities within this request. For example, you select 5 temporary tables from your database, change some values ​​in the first object and delete the last one. DbContext knows that the first object is changed, and the last is deleted, if you call SaveChanges , DbContext will automatically update the first object, delete the last one and leave the others untouched. Now you are trying to update the object yourself and reattach the first object to the DbContext.

Now DbContext has two sides with the same key, and this will result in your exception.

For 3 .: DynamicProxies is an object that the entity infrastructure uses to track changes to these objects.

For 4 .: check the link , there is also a good book about the entity framework 6 (Title: "Programming the entity structure")

+2
source share

All Articles