本文介绍了EF核心清除项目在交易中未删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的标记

  using(var db = new CassaContext()){
var DbSet = db。 GetByName方法(item.Name);
DbSet.RemoveRange(DbSet);

for(var i = 0; i< data.Count; i ++)
{
JObject jitem = data [i] as JObject;
var dbobj = vm.Db.JsonToObject(jitem);
DbSet.Add(dbobj);
}

db.SaveChanges();
}

如果我将在RemoveRange()istruction之后添加db.SaveChanges()一切都很好,但没有,我有错误

I想避免在删除之后保存,因为如果事务失败,我不会有空表。



我该如何解决?提前致谢!
如果你需要更多的只是问!

解决方案

不知道这个错误,但你可以尝试使用一个明确的交易使用(var db = new CassaContext())
使用(var transaction = db.Database.BeginTransaction() ))
{
var DbSet = db.GetByName(item.Name);
DbSet.RemoveRange(DbSet);
db.SaveChanges();

for(var i = 0; i< data.Count; i ++)
{
JObject jitem = data [i] as JObject;
var dbobj = vm.Db.JsonToObject(jitem);
DbSet.Add(dbobj);
}
db.SaveChanges();

transaction.Commit();
}


Here's my prblem

using (var db = new CassaContext()) {        
      var DbSet = db.GetByName(item.Name);
      DbSet.RemoveRange(DbSet);

      for (var i = 0; i < data.Count; i++)
      {
           JObject jitem = data[i] as JObject;
           var dbobj = vm.Db.JsonToObject(jitem);             
           DbSet.Add(dbobj);
       }

       db.SaveChanges();
}

If i will add db.SaveChanges() after the RemoveRange() istruction, everything is fine, but, without that, i have the error

I would like to avoid saving after the removerange, because i won't have empty tables if the transaction fails.

How can i solve this? Thanks in advance!If you need more just ask!

解决方案

Not sure about this error, but you could try using an explicit transaction:

using (var db = new CassaContext())
using (var transaction = db.Database.BeginTransaction())
{
    var DbSet = db.GetByName(item.Name);
    DbSet.RemoveRange(DbSet);
    db.SaveChanges();

    for (var i = 0; i < data.Count; i++)
    {
        JObject jitem = data[i] as JObject;
        var dbobj = vm.Db.JsonToObject(jitem);             
        DbSet.Add(dbobj);
    }
    db.SaveChanges();

    transaction.Commit();
}

这篇关于EF核心清除项目在交易中未删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:05