I'm working with EF6 code first
in a WinForm
project.
I used following method for reading entities from Db
, updating them and then save back them to Db
:
- Read Entity graph using
Linq to entities
(after reading theDbContext
disposes) - Show readed
Entity
graph to end user. - End user may apply this changes to
Entity
graph:- Update root entity
- Add some child entities
- Edit some child entities
- Delete some child Entities
- User call a method to persist his changes to
Db
- Create a new
DbContext
instance. - Reload same
Entity
's graph fromDb
- Map the all property's value from user entity to reloaded entity using
AutoMapper
- Attach the result entity of 6 step to my
DbContext
usingGraphDiff
Call
DbContext.SaveChanges();
to persist changes toDb
var root = new MyDbcontext() .Roots .LoadAggregation() .ToList(); // LoadAggregation in this case, means following codes: // .Include("Child1") // .Include("Child2") root.Child1s.Remove(child11); root.Child1.Add(Child13); // root.Child2.Add(Child22); using(var uow = new UnitOfWork()) { uow.Repository<Root>().Update(root); uow.Repository<AnotherRoot>().Update(anotherRoot); //user may want to update multiple Roots uow.SaveChanges(); <---- at this point Child13.Id and Child22.Id generated by Db }
public void Update(Root entity) //Update method in my Repository class
{
var context = new MyDbcontext();
var savedEntity = context.Roots //reload entity graph from db
.LoadAggregation()
.ToList();
Mapper.Map(entity,savedEntity); // map user changes to original graph
context.UpdateGraph(savedEntity, savedEntity.MappingConfiguration); // attach updated entity to dbcontext using graphdiff
}
public void SaveChanges() // SaveChanges() in UnitofWork class
{
context.SaveChanges();
}
It works fine,
In second graph the Child13 and Child22 added by user and when I call uow.SaveChanges()
they will save to Db
and their Id
s will be assign. but Child13.Id
and Child22.Id
in entity
objects are 0
yet, I could manually update the Id
s but I'm looking for generic way to update these Id
values with Db
generated Id
s.