首页
视频
资源
登录
转
EF CORE 6使用事务。entity framework
2345
人阅读
2023/2/24 9:41
总访问:
588302
评论:
0
收藏:
0
手机
分类:
ORM
如果出现错误:`The connection is already in a transaction and cannot participate in another transaction.`可以进行一下判断 ``` var transaction = Database.CurrentTransaction ?? Database.BeginTransaction() ``` Try to use this helper function for creating a new transaction: ``` public CommittableTransaction CreateTransaction() => new System.Transactions.CommittableTransaction(new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }); ``` Using the Northwind database as example database, you can use it like: ``` public async Task<int?> CreateCategoryAsync(Categories category) { if (category?.CategoryName == null) return null; using(var trans = CreateTransaction()) { await this.Context.Categories.AddAsync(category); await this.Context.SaveChangesAsync(); trans.Commit(); return category?.CategoryID; } } ``` And then you can call it from another function like: ``` /// <summary>Create or use existing category with associated products</summary> /// <returns>Returns null if transaction was rolled back, else CategoryID</returns> public async Task<int?> CreateProjectWithStepsAsync(Categories category) { using var trans = CreateTransaction(); int? catId = GetCategoryId(category.CategoryName) ?? await CreateCategoryAsync(category); if (!catId.HasValue || string.IsNullOrWhiteSpace(category.CategoryName)) { trans.Rollback(); return null; } var product1 = new Products() { ProductName = "Product A1", CategoryID = catId }; await this.Context.Products.AddAsync(product1); var product2 = new Products() { ProductName = "Product A2", CategoryID = catId }; await this.Context.Products.AddAsync(product2); await this.Context.SaveChangesAsync(); trans.Commit(); return catId; } ``` To run this with LinqPad you need an entry point (and of course, add the NUGET package EntityFramework 6.x via F4, then create an EntityFramework Core connection): ``` // Main method required for LinqPad UserQuery Context; async Task Main() { Context = this; var category = new Categories() { CategoryName = "Category A1" // CategoryName = "" }; var catId = await CreateProjectWithStepsAsync(category); Console.WriteLine((catId == null) ? "Transaction was aborted." : "Transaction successful."); } ``` This is just a simple example - it does not check if there are any product(s) with the same name existing, it will just create a new one. You can implement that easily, I have shown it in the function CreateProjectWithStepsAsync for the categories: ``` int? catId = GetCategoryId(category.CategoryName) ?? await CreateCategoryAsync(category); ``` First it queries the categories by name (via GetCategoryId(...)), and if the result is null it will create a new category (via CreateCategoryAsync(...)). Also, you need to consider the isolation level: Check out System.Transactions.IsolationLevel to see if the one used here (ReadCommitted) is the right one for you (it is the default setting). What it does is creating a transaction explicitly, and notice that here we have a transaction within a transaction. 原文:https://stackoverflow.com/questions/70958670/how-to-nest-transactions-in-ef-core-6
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
TeachingNote
这一生多幸运赶上过你.
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net
37篇
后端
5篇
ORM
16篇
前端
48篇
随笔
13篇
移动开发
6篇
数据库
6篇
英语
3篇
rabbitmq
1篇
.net core
5篇
unity
1篇
k8s
1篇
云服务
1篇
DDD
1篇
网络
1篇
更多
mysql
1篇
python
2篇
docker
1篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术