cascade operation in transaction scope failed
Posted: Thu 04 Aug 2011 11:02
Hi,
I have a problem and i can not resolve it when I used cascade operation in a transaction scope.
TARIFE entity has a child entity is called TARIFEARACTIP.
In that code you can see below I try to delete a TARIFE record. In order to this I need to delete all related data at TARIFEARACTIP.
At second savechanges statement I got an exception said:
TARIFE has a child rows.
However, I am deleting all TARIFEARACTIPs using this code . It did not worked;
foreach (var item in result)
{
context.TARIFEARACTIPs.DeleteObject(item);
}
context.SaveChanges();
Code snip:
using (TransactionScope ts = new TransactionScope())
{
using (DataEntities context = new DataEntities(Global.CONNECTIONSTRING_NAME))
{
TARIFE tarife = (from item in context.TARIFEs
where item.TARIFEID == tarifeID
select item).First();
var result = (from item in context.TARIFEARACTIPs
where item.TARIFEID == tarife.TARIFEID
select item).ToList();
foreach (var item in result)
{
context.TARIFEARACTIPs.DeleteObject(item);
}
context.SaveChanges();
context.TARIFEs.DeleteObject(tarife);
context.SaveChanges();
ts.Complete();
}
}
I have a problem and i can not resolve it when I used cascade operation in a transaction scope.
TARIFE entity has a child entity is called TARIFEARACTIP.
In that code you can see below I try to delete a TARIFE record. In order to this I need to delete all related data at TARIFEARACTIP.
At second savechanges statement I got an exception said:
TARIFE has a child rows.
However, I am deleting all TARIFEARACTIPs using this code . It did not worked;
foreach (var item in result)
{
context.TARIFEARACTIPs.DeleteObject(item);
}
context.SaveChanges();
Code snip:
using (TransactionScope ts = new TransactionScope())
{
using (DataEntities context = new DataEntities(Global.CONNECTIONSTRING_NAME))
{
TARIFE tarife = (from item in context.TARIFEs
where item.TARIFEID == tarifeID
select item).First();
var result = (from item in context.TARIFEARACTIPs
where item.TARIFEID == tarife.TARIFEID
select item).ToList();
foreach (var item in result)
{
context.TARIFEARACTIPs.DeleteObject(item);
}
context.SaveChanges();
context.TARIFEs.DeleteObject(tarife);
context.SaveChanges();
ts.Complete();
}
}