首页 > 解决方案 > MVC 实体框架回滚不起作用

问题描述

嗨,我正在尝试在我的应用程序中使用在 MVC.net 中编码的事务我在一个层中有我的 dbcontext,我从业务层到达它。当我运行代码时,它没有给我任何错误,但它也根本没有进行交易。

这是我的背景

public class DB : IDisposable
{
    public DB()
    {


    }

    private RootDYSContext _ctx = null;
    public RootDYSContext ctx
    {
        get
        {
            if (_ctx == null)
                _ctx = new RootDYSContext();
            return _ctx;
        }
        set
        {
            _ctx = value;
        }
    }
    public bool Commit()
    {
        try
        {
            ctx.SaveChanges();
        }
        catch (Exception exp)
        {
            Mesaj = exp.Message;
            return false;
        }
        return true;

    }
}

这就是我尝试根据此处“ https://docs.microsoft.com/en-us/ef/core/saving/transactions ”进行交易的方式

    public bool CreateKullanici(VMKullanici 
    KullaniciData,List<VMRol>RolDataList,VMRol AnaRolData)
    {
        string mesaj = "";
        int kullanici_id;

        using (RootDBHelper.DB db = new RootDBHelper.DB())
        {

            using (var ctx = new RootDBLayer.RootDYSContext())
            {
                using (DbContextTransaction dbContextTransaction = 
                   ctx.Database.BeginTransaction())
                {
                    try
                    {
                        kullanici_id = SaveKullanici(KullaniciData, out 
                   mesaj);
                        foreach(VMRol rol in RolDataList)
                        {
                            VMKullaniciRol KullaniciRolInsertData = new 
                                  VMKullaniciRol();
                            KullaniciRolInsertData.kullanici_id = 
                              kullanici_id;
                            KullaniciRolInsertData.rol_id = rol.rol_id;

                            if (rol.rol_id == AnaRolData.rol_id)
                                KullaniciRolInsertData.ana_rol_mu = 
                                   (int)RConstants.EvetHayir.Evet;
                            else
                                KullaniciRolInsertData.ana_rol_mu = 
                             (int)RConstants.EvetHayir.Hayir;


                bKullaniciRol.SaveKullaniciRol(KullaniciRolInsertData, out mesaj);
                        }
/*Edit:I think this line made confusion I put this to make sure rollback 
                        dbContextTransaction.Rollback(); */
                     dbContextTransaction.Commit();

                        return true;
                    }
                    catch
                    {
                        dbContextTransaction.Rollback();
                        return false;
                    }

                }
            }
        }
    }

我希望当我运行它时,我根本不应该得到任何记录。我严重卡住了,因为我根本没有收到任何错误或任何提示,而且无论回滚如何,这种方法都会保存数据。先感谢您。

编辑:我在插入方法上进行了保存更改部分还为方法添加了代码示例

    public int SaveKullanici(VMKullanici KullaniciData, out string mesaj)
    {
        using (RootDBHelper.DB db = new RootDBHelper.DB())
        {
            mesaj = "";
            PKullanici pKullanici = new PKullanici();
            if (KullaniciData.kullanici_id == default(int))
                pKullanici.InsertKullanici(db.ctx, KullaniciData);
            else
                pKullanici.UpdateKullanici(db.ctx, KullaniciData);
            if (db.Commit())
            {
                return KullaniciData.kullanici_id;
            }
            else
            {
                mesaj = db.Mesaj;
                return -1;
            }
        }
    }



    public void InsertKullanici(RootDYSContext ctx, VMKullanici KullaniciData)
    {
        ctx.TKullanici.Add(KullaniciData.TKullanici);
    }

标签: c#asp.net-mvcentity-framework

解决方案


我认为您的问题是您确实提交了然后您想回滚,但是您应该提交或回滚一件事。

此外,我无法在您的代码中看到您实际执行 transaction.commit 的位置。您的代码具有提交功能,但提交!= savechanges。

事务的想法(有不同的类型,但是)是您将数据保存到数据库中进行一些验证或操作,然后您说它看起来不错,提交或有错误执行回滚。

另请注意,如果您不是不提交或回滚,那么无论如何您的身份字段都会增加


推荐阅读