[转]ASP.NET Core 1.0: Using Entity Framework Core 1.0 - Transaction
本文转自:http://blog.csdn.net/alvachien/article/details/51576961
跟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、ASP.NET MVC Core 1.0和Entity Framework Core 1.0
ASP.NET 5.0 将改名为 ASP.NET Core 1.0 ASP.NET MVC 6 将改名为 ASP.NET MVC Core 1.0 Entity Framework 7.0 将 ...
- [转帖]2016年时的新闻:ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0
ASP.NET Core 1.0.ASP.NET MVC Core 1.0和Entity Framework Core 1.0 http://www.cnblogs.com/webapi/p/5673 ...
- ASP.NET Core 1.0: Using Entity Framework Core
伴随着ASP.NET Core 1.0发布的还有Entity Framework Core 1.0; 官方文档链接:https://docs.efproject.net/en/latest/platf ...
- Professional C# 6 and .NET Core 1.0 - 38 Entity Framework Core
本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 38 Entity Framework ...
- ASP.NET Core 1.0: Using Entity Framework Core 1.0 - Transaction
跟Entity Framework之前的版本不同,Class DbContext不再有AcceptAllChanges()方法. 使用Transaction需要使用DbContext中的Databas ...
- 请问在 .NET Core 中如何让 Entity Framework Core 在日志中记录由 LINQ 生成的SQL语句?
using dotNET.Core; using Microsoft.Extensions.Logging; using System; using System.Collections.Generi ...
- Entity Framework Core 练习参考
项目地址:https://gitee.com/dhclly/IceDog.EFCore 项目介绍 对 Microsoft EntityFramework Core 框架的练习测试 参考文档教程 官方文 ...
- 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)
在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...
- ASP.Net Core项目在Mac上使用Entity Framework Core 2.0进行迁移可能会遇到的一个问题.
在ASP.Net Core 2.0的项目里, 我使用Entity Framework Core 2.0 作为ORM. 有人习惯把数据库的连接字符串写在appSettings.json里面, 有的习惯写 ...
随机推荐
- resteasy json
https://www.cnblogs.com/toSeeMyDream/p/5763725.html
- python 几种循环性能测试: while, for, 列表生成式, map等
直接上代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/07/24 16:23 import itertools imp ...
- [知识复习] C语言文件读写
文件打开 fopen() 返回FILE* 对象,如果打开失败返回NULL,错误代码存入errno中 FILE *fopen( const char * filename, const char * m ...
- AC日记——严酷的训练 洛谷 P2430
严酷的训练 思路: 背包: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 5005 int n,m,bi[m ...
- 在C#中使用正则表达式筛选出图片URL并下载图片URL中的图片到本地
本功能主要用到的知识点如下: 1.正则表达式 2.C#中下载文件功能的实现 3.泛型集合的使用 4.进程的简单操作(用于结束当前程序) 下面就简单说一下是如何使用这些知识点的.先详细说下这个程序主要实 ...
- C++中正确使用PRId64
http://blog.csdn.net/win_lin/article/details/7912693
- buntu 16.04上安装和配置Samba服务器
https://www.linuxidc.com/Linux/2017-11/148194.htm
- hdu 1698 Just a Hook(线段树区间修改)
传送门:Just a Hook Problem Description In the game of DotA, Pudge’s meat hook is actually the most horr ...
- 用python正则表达式提取网页的url
import re import urllib url="http://www.itokit.com" s=urllib.urlopen(url).read() ss=s.repl ...
- Win7 + VirtualBox + CentOS(无桌面), 扩容
http://www.2cto.com/os/201401/269730.html 对于目前的网络开发者来说,比较好的搭档就是Win7+VirtualBox+CentOS的组合,既可以发挥Linux强 ...