[转]ASP.NET Core 1.0: Using Entity Framework Core 1.0 - Transaction
2017-02-21 10:25
543 查看
本文转自:http://blog.csdn.net/alvachien/article/details/51576961
跟Entity Framework之前的版本不同,Class DbContext不再有AcceptAllChanges()方法。
使用Transaction需要使用DbContext中的Database对象。
当然要使用async,必须将对应的Method做调整
为:
值得强调一下的是,AcceptAllChanges()方法还是存在的,只不过移到了ChangeTracker上了。 看看DbContext的定义
使用AcceptAllChanges()的实例:
使用ChangeTracker.AcceptAllChanges()方法有个问题,就是设置为Identity的Column不会自动获取ID,因为SaveChanges(false)表示不向数据库提交修改。对SQL Server来说,只有SaveChanges(),EntityFramework才会调用SCOPE_IDENTITY()来获取下一个ID。
跟Entity Framework之前的版本不同,Class DbContext不再有AcceptAllChanges()方法。
使用Transaction需要使用DbContext中的Database对象。
using (var transaction = await _dbContext.Database.BeginTransactionAsync()) { try { ... Operation on object _dbContext.TableA.Add(rowa); // Add rowa to Table A _dbContext.SaveChanges(); _dbContext.TableB.Add(rowb); // Add rowb to Table B _dbContext.SaveChanges(); transaction.Commit(); } catch (Exception exp) { #if DEBUG Console.WriteLine(exp.Message); #endif transaction.Rollback(); }
当然要使用async,必须将对应的Method做调整
[HttpPost] public IActionResult Create([FromBody] MyViewModel ch)
为:
[HttpPost] public async Task<IActionResult> Create([FromBody] MyViewModel ch)
值得强调一下的是,AcceptAllChanges()方法还是存在的,只不过移到了ChangeTracker上了。 看看DbContext的定义
public class DbContext : IDisposable, IInfrastructure<IServiceProvider> { public DbContext([NotNullAttribute] DbContextOptions options); protected DbContext(); public virtual ChangeTracker ChangeTracker { get; } public virtual DatabaseFacade Database { get; } ... }
使用AcceptAllChanges()的实例:
// Some tables changed _dbContext.SaveChanges(false); // Other tables changed _dbContext.SaveChanges(false); // Now save it. _dbContext.ChangeTracker.AcceptAllChanges();
使用ChangeTracker.AcceptAllChanges()方法有个问题,就是设置为Identity的Column不会自动获取ID,因为SaveChanges(false)表示不向数据库提交修改。对SQL Server来说,只有SaveChanges(),EntityFramework才会调用SCOPE_IDENTITY()来获取下一个ID。
相关文章推荐
- ASP.NET Core 1.0: Using Entity Framework Core 1.0 - Transaction
- ASP.NET Core 1.0: Using Entity Framework Core
- ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0
- 使用 ASP.NET Core, Entity Framework Core 和 ABP 创建N层Web应用 第二篇
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-3
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-5
- 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序
- http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
- ASP.NET Core 开发 - Entity Framework (EF) Core
- Asp.net Core 2.0+EntityFrameWorkCore 2.0添加数据迁移
- ASP.NET CORE系列【三】使用Entity Framework Core进行增删改查
- ASP.NET CORE系列【六】Entity Framework Core 之数据迁移
- asp.net Core Entity Framework 7 数据库更新维护
- 使用 Visual Studio 的 ASP.NET Core MVC 和 Entity Framework Core 入门 (1 of 10)
- Asp.net Core 2.0+EntityFrameWorkCore 2.0添加数据迁移
- ASP.NET CORE系列【二】使用Entity Framework Core进行增删改查
- [转]Using the Repository Pattern with ASP.NET MVC and Entity Framework
- Asp .net core api+Entity Framework core 实现数据CRUD数据库中(附Git地址)
- 源码学习之ASP.NET MVC Application Using Entity Framework
- 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序