Reputation: 147
I am using WCF DataService and the AutoMapper to Map the objects.
The below sample works fine when I get the single entity.
BOND contextBond = this.agent.Context.BONDS.Where(x => x.SECURITY_ID == Security.SECURITY_ID).FirstOrDefault();
Mapper.Map<BondItem, BOND>(this.Bond, contextBond);
this.agent.Context.UpdateObject(contextBond);
The below is the implementation for List Object. I am getting an exception "The context is not currently tracking the entity." at the last line
List<SECURITY_XREF> contextXREF = this.agent.Context.SECURITY_XREF.Where(x => x.SECURITY_ID == Security.SECURITY_ID).ToList();
Mapper.Map<List<SecurityXrefItem>, List<SECURITY_XREF>>(this.Xref, contextXREF);
this.agent.Context.UpdateObject(contextXREF);
Any Idea How can I resolve the issue? In other words how can I pass the List Objects to the UpdateObject Methods.
Thx
Upvotes: 1
Views: 8098
Reputation: 353
foreach (var entity in list)
{
if(!context.IsAttached(entity)) context.Attach(entity);
context.UpdateObject(entity);
}
context.SaveChanges()
Upvotes: 1
Reputation: 153
The DataServiceContext.UpdateObject only accept single entity object, you have to call it once on each object in the list. DataServiceContext.SaveChanges will save all changes to server with one call.
foreach (var entity in list)
{
context.UpdateObject(entity);
}
context.SaveChanges()
Upvotes: 2