尝试使用实体框架执行复杂过程并在事务表中保存数据。当我的代码提交事务时,它会抛出异常
无法执行交易操作,因为有待处理此请求的待处理请求。
以下是代码。
[
{ "id": 1,
"agent": {
"id": 1,
"firstName": "gghg",
"lastName": "gh",
"phone": "4543534",
"admin": true
},
"user":"agent@gmail.com"
},
{ "id": 2,
"agent": {
"id": 1,
"firstName": "gghg",
"lastName": "gh",
"phone": "4543534",
"admin": true
},
"user":"agent1@gmail.com"
}
]
不知道为什么它会抛出待处理请求异常,因为我在同一个事务中执行它。
以下是完整的堆栈跟踪
using (abcEntities dbContext = new abcEntities())
{
using (var transaction = dbContext.Database.BeginTransaction())
{
dbContext.USP_ResellerCustomerFile_DeleteFiles(customerId, fileId, 1);
ResellerAndCustomerActivityDAL objCustomerActivityDAL = new ResellerAndCustomerActivityDAL();
ResellerAndCustomerActivity objActivity = new ResellerAndCustomerActivity()
{
Activity = objfile.FileName + " is temporairly permanently deleted.",//message.ReplacingSpecialCharacterswithEntities(),
ActivityDate = DateTime.UtcNow,
ResellerAndCustomerId = customerId,
UserTypeId = 2
};
dbContext.ResellerAndCustomerActivities.Add(objActivity);
//objCustomerActivityDAL.Create(objfile.FileName + " is permanently deleted.", objUnit, out returnMessage, customerId);
dbContext.SaveChanges();
transaction.Commit();
}
}
答案 0 :(得分:0)
我找到了另一种方式。
而不是直接从dbcontext调用存储过程我称之为 的 dbContext.Database.DbRawSqlQuery 即可。现在我按照以下顺序做了以上所有事情
甚至不需要创建事务,因为默认情况下所有实体(与数据库相关)语句都在事务中执行。可以阅读here