小编Ele*_*uge的帖子

EntityFramework Transaction - 保存到多个表

如果保存时有任何异常,下面的代码是否会回滚更改?

using (SampleEntities context = new SampleEntities())
{
    //Code Omitted

    context.EmpPercAdjustments.AddRange(pp);
    context.SampleJobs.AddRange(sampleJobs);

    context.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)

要么

我需要使用交易吗?

using (SampleEntities context = new SampleEntities())
{
    //Code Omitted

    using (System.Data.Entity.DbContextTransaction tranc = context.Database.BeginTransaction( ))
    {
        try
        {
            context.EmpPercAdjustments.AddRange(pp);
            context.SampleJobs.AddRange(sampleJobs);
            context.SaveChanges();
            tranc.Commit();
        }
        catch (Exception ee)
        {
            tranc.Rollback();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用一个优于其他优点是否有任何优势?

c# entity-framework

3
推荐指数
1
解决办法
2736
查看次数

标签 统计

c# ×1

entity-framework ×1