public void SomeMethod1()
{
using (TemplateEntities ctx = new TemplateEntities())
{
//do something in this ctx
}
}
public void SomeMethod2()
{
using (TemplateEntities ctx = new TemplateEntities())
{
//do something else in this ctx
}
}
public void SomeMethod()
{
using (TemplateEntities ctx = new TemplateEntities())
{
using (TransactionScope tran = new TransactionScope())
{
SomeMethod1();
SomeMethod2();
var itemToDelete= (from x in ctx.Xxx
where x.Id==1
select x).Single();
ctx.Xxx.DeleteObject(itemToDelete);
ctx.SaveChanges();
tran.Complete();
}
}
}
即使有更多的上下文,SomeMethod中會發生什麼? 我正在使用POCO。關於實體框架和事務的問題
但是,如果我們正在談論與一個數據庫表是正確的我的例子? – gigi 2010-10-23 20:58:12
不,它仍然需要MSDTC。但是,當您管理自己的連接時,這不會成爲問題。有關更多信息,請參見http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/189b2718-c2b0-4290-8517-3cd3da4314fc。 – 2010-10-23 21:45:43