首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于entity framework中的事物处置

2013-01-17 
关于entity framework中的事物处理如果我在DAL 层写了对每个表的插入语句的方法的封装例如 public void in

关于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 

热点排行