日期:2014-05-17  浏览次数:20862 次

关于entity framework中的事物处理
如果我在DAL 层写了对每个表的插入语句的方法的封装例如
 public void insert()
{
 try
        {
            TB_Cars c = new TB_Cars();
            using (carEntities aab = new carEntities())
            {
                
                aab.AddObject("TB_Cars", c);
                aab.SaveChanges();
                
            }
        }
        catch (Exception ex)
        {
            throw ex.InnerException;
        }
}
 public void insert2()
{
 TB_types c = new TB_types();
                try
        {
            using (carEntities aab = new carEntities())
            {
                aab.AddObject("TB_types", c);
                aab.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            throw ex.InnerException;
        }
}
现在 我想新建一个类 
class InsertTwo
{
public void inert()
{
 try
        {
            haha.insert2();
            haha.insert();



        }

}
}
但是如果  第一个 insert2 成功了,第二个insert();
 失败了的话,我想让数据库回滚,但是事务处理 要怎么写啊,是写在这个类中吗,还是怎么办啊,求大神指导

------解决方案--------------------
可以考虑 单元模式,EF更容易,只要carEntities aab 是同一个实例 就行,到最后SaveChanges