EF 分布式事务的使用
程序员文章站
2022-07-13 22:36:51
...
using (JDDbContext dbContext = new JDDbContext())
{
using (TransactionScope trans = new TransactionScope())
{
User userNew1 = new User()
{
Account = "Admin",
State = 0,
CompanyId = 2031,
CompanyName = "地府",
CreateTime = DateTime.Now,
CreatorId = 1,
Email = "[email protected]",
LastLoginTime = null,
LastModifierId = 0,
LastModifyTime = DateTime.Now,
Mobile = "18664876671",
Name = "啦啦啦32fdsdfds",
Password = "12356789",
UserType = 1
};
dbContext.Set<User>().Add(userNew1);
dbContext.SaveChanges();
SysLog sysLog = new SysLog()
{
CreateTime = DateTime.Now,
CreatorId = userNew1.Id,
LastModifierId = 0,
LastModifyTime = DateTime.Now,
Detail = "12345678",
Introduction = "sadsfghj",
LogType = 1,
UserName = "zhangsanan2zhangsanan2zhangsanan2zhangsanan2zhangsanan2zhangsanan2"
};
dbContext.Set<SysLog>().Add(sysLog);
dbContext.SaveChanges();
trans.Complete();//能执行这个,就表示成功了;
}
}
using (JDDbContext dbContext1 = new JDDbContext())
using (JDDbContext dbContext2 = new JDDbContext())
{
using (TransactionScope trans = new TransactionScope())
{
User userNew1 = new User()
{
Account = "Admin",
State = 0,
CompanyId = 2031,
CompanyName = "地府",
CreateTime = DateTime.Now,
CreatorId = 1,
Email = "[email protected]",
LastLoginTime = null,
LastModifierId = 0,
LastModifyTime = DateTime.Now,
Mobile = "18664876671",
Name = "开导开导开导",
Password = "12356789",
UserType = 1
};
dbContext1.Set<User>().Add(userNew1);
dbContext1.SaveChanges();
SysLog sysLog = new SysLog()
{
CreateTime = DateTime.Now,
CreatorId = userNew1.Id,
LastModifierId = 0,
LastModifyTime = DateTime.Now,
Detail = "12345678",
Introduction = "sadsfghj",
LogType = 1,
UserName = "zhangsanan2zhangsanan2zhangsanan2zhangsanan2333333333"
};
dbContext2.Set<SysLog>().Add(sysLog);
dbContext2.SaveChanges();
trans.Complete();//能执行这个,就表示成功了;
}
}
上一篇: Ext Combobox
推荐阅读
-
sql事务的使用及其技巧整理
-
从零开始搭建前后端分离的NetCore2.2(EF Core CodeFirst+Autofac)+Vue的项目框架之七使用JWT生成Token(个人见解)
-
从零开始搭建前后端分离的NetCore2.2(EF Core CodeFirst+Autofac)+Vue的项目框架之十一Swagger使用一
-
关于分布式事务的实现梳理
-
python django框架中使用FastDFS分布式文件系统的安装方法
-
SQL中的事务、特点、特性、使用介绍
-
使用Python的Django框架实现事务交易管理的教程
-
SQLServer2005触发器提示其他会话正在使用事务的上下文的解决方法
-
SQLSERVER分布式事务使用实例
-
spring事务的@Transactional使用事务不生效问题