LinqToSql - ChangeConflictException. when submiting child and parent
Posted
by ari
on Stack Overflow
See other posts from Stack Overflow
or by ari
Published on 2010-05-09T05:32:11Z
Indexed on
2010/05/09
5:38 UTC
Read the original article
Hit count: 434
linq-to-sql
This problem drives me crazy. Here's the code
using (BizNetDB db = new BizNetDB())
{
var dbServiceCall = db.ServiceCalls.SingleOrDefault(x => x.ServiceCallID == serviceCallDetail.ServiceCallID);
var dbServiceCallDetail = dbServiceCall.ServiceCallDetaills.SingleOrDefault(x=> x.ServiceCallDetailID == serviceCallDetail.ServiceCallDetailID);
if (dbServiceCallDetail == null)
{
dbServiceCallDetail = new Data.ServiceCallDetaill();
dbServiceCall.ServiceCallDetaills.Add(dbServiceCallDetail);
}
dbServiceCallDetail.EndSession = serviceCallDetail.EndSession;
dbServiceCallDetail.ExitTime = serviceCallDetail.ExitTime;
dbServiceCallDetail.Solution = serviceCallDetail.Solution;
dbServiceCallDetail.StartSession = serviceCallDetail.StartSession;
serviceCallDetail.SessionMinutes = (serviceCallDetail.EndSession - serviceCallDetail.StartSession).Minutes;
serviceCallDetail.DriveMinutes = serviceCallDetail.ExitTime.HasValue ? (serviceCallDetail.StartSession - serviceCallDetail.ExitTime.Value).Minutes : 0;
var totalMinutes = (from d in db.ServiceCallDetaills
.Where(x => x.ServiceCallID == serviceCallDetail.ServiceCallID && x.ServiceCallDetailID != dbServiceCallDetail.ServiceCallDetailID)
group d by d.ServiceCallID into g
select new { SessionMinutes = g.Sum(x => x.SessionMinutes), DriveMinutes = g.Sum(x => x.DriveMinutes) }).First();
dbServiceCall.SessionMinutes = totalMinutes.SessionMinutes + serviceCallDetail.SessionMinutes;
dbServiceCall.DriveMinutes = totalMinutes.DriveMinutes + serviceCallDetail.DriveMinutes;
try
{
db.SubmitChanges();
}
catch (ChangeConflictException ex)
{
db.ChangeConflicts.ResolveAll(RefreshMode.OverwriteCurrentValues);
db.SubmitChanges();
}
The second Submit did solve the problem.. but I want to solve it from the root!
when I disabled this lines (The parent changes):
dbServiceCall.SessionMinutes = totalMinutes.SessionMinutes + serviceCallDetail.SessionMinutes;
dbServiceCall.DriveMinutes = totalMinutes.DriveMinutes + serviceCallDetail.DriveMinutes;
everithing is Ok.
please help...
© Stack Overflow or respective owner