mysql - 为什么我使用 nhibernate 的 asp.net-mvc 站点会停止更新和删除?

标签 mysql asp.net-mvc nhibernate fluent-nhibernate ninject

我有一个非常简单的 CRUD asp.net-mvc 站点,它使用 nhibernate 与 mySQL 数据库进行交互。我正在使用 UnitOfWork 和 Repository 模式。升级到 MVC 4 和最新的 nhibernate 和 mySQL 版本(通过 nuget)后,我突然看到一个奇怪的问题,更新和删除已停止工作。

下面是一个在我的 Controller 中停止工作的删除代码示例:

    public ActionResult Delete(int id)
    {
        MyEvent c = _eventRepository.FindBy(id);

        _unitOfWork.Begin();
        _eventRepository.Delete(c);
        _unitOfWork.End();

        return RedirectToAction("Index");
    }

UnitOfWork 代码如下所示:

    public UnitOfWork(ISessionFactory sessionFactory)
    {
        _sessionFactory = sessionFactory;
        Session = _sessionFactory.OpenSession();
        Session.FlushMode = FlushMode.Auto;
    }

   public void End()
    {
        Commit();
        if (Session.IsOpen)
        {
            Session.Close();
        }
    }

    public void Commit()
    {
        if (!_transaction.IsActive)
        {
            throw new InvalidOperationException("No active transation");
        }
        _transaction.Commit();
    }

    public void Begin()
    {
        _transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
    }

我测试了添加一个工作正常的新项目(新行显示在数据库表中)但是当我测试更新或删除时,代码运行正常(我没有在代码中得到任何异常)但是字段是当我执行更新时没有被更新,并且当我运行删除代码时记录没有被删除。

所以回顾一下,从 mySQL 数据库读取数据工作正常,添加工作正常,但更新和删除已停止对所有表工作(之前工作)。我使用 Toad for MySQL 对常规 SQL 进行了测试,效果很好(使用我在代码中连接时使用的相同登录凭据)

为了进一步帮助调试,我启动了 nhibernate profiler,这是我看到的删除或更新条目:

enter image description here

这是我在加载常规阅读页面时看到的:

enter image description here

不确定这是否有助于解释问题,但我认为添加屏幕截图不会有什么坏处。

关于可能发生的事情的任何建议。这可能是一个权利问题(相对于某些软件库错误?)。同样,如上所述,这段代码以前确实有效。

这是我的 Ninject Ioc 代码:

        string connectionString = ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString;

        var helper = new NHibernateHelper(connectionString);
        Bind<ISessionFactory>().ToConstant(helper.SessionFactory)
            .InSingletonScope();

        Bind<IUnitOfWork>().To<UnitOfWork>();

        var sessionProvider = new SessionProvider();
        Bind<ISession>().ToProvider(sessionProvider);

        var unitOfWork = new UnitOfWork(helper.SessionFactory);

        Bind(typeof(IIntKeyedRepository<>)).To(typeof(Repository<>));
    }

这是我的 unitofwork.cs 代码:

public class UnitOfWork : IUnitOfWork
{
    private readonly ISessionFactory _sessionFactory;
    private ITransaction _transaction;

    public ISession Session { get; private set; }

    public UnitOfWork(ISessionFactory sessionFactory)
    {
        _sessionFactory = sessionFactory;
        Session = _sessionFactory.OpenSession();
        Session.FlushMode = FlushMode.Auto;
    }

    public void End()
    {
        Commit();
        if (Session.IsOpen)
        {
            Session.Close();
        }
    }

    public void Begin()
    {
        _transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
    }

    public void Dispose()
    {
        if (Session.IsOpen)
        {
            Session.Close();
        }
    }

    public void Commit()
    {
        if (!_transaction.IsActive)
        {
            throw new InvalidOperationException("No active transation");
        }
        _transaction.Commit();
    }

    public void Rollback()
    {
        if (_transaction.IsActive)
        {
            _transaction.Rollback();
        }
    }
}

这是我的存储库代码:

public class Repository<T> : IIntKeyedRepository<T> where T : class
{
    private readonly ISession _session;
    private ITransaction _trans;

    public T FindBy(int id)
    {
        return _session.Get<T>(id);
    }

    public Repository(ISession session)
    {
        _session = session;
    }

    public bool Add(T entity)
    {
        _session.Save(entity);
        return true;
    }

    public bool Add(IEnumerable<T> items)
    {
        foreach (T item in items)
        {
            _session.Save(item);
        }
        return true;
    }

    public bool Update(T entity)
    {
        _session.Update(entity);
        return true;
    }

    public bool Delete(T entity)
    {
        _session.Delete(entity);
        return true;
    }

    public bool Delete(IEnumerable<T> entities)
    {
        foreach (T entity in entities)
        {
            _session.Delete(entity);
        }
        return true;
    }

    #endregion

    #region IIntKeyedRepository<T> Members

    public T FindBy(int id)
    {
        return _session.Get<T>(id);
    }

    #endregion

    #region IReadOnlyRepository<T> Members

    public IQueryable<T> All()
    {
        return _session.Query<T>();
    }

    public T FindBy(Expression<Func<T, bool>> expression)
    {
        return FilterBy(expression).Single();
    }

    public IQueryable<T> FilterBy(Expression<Func<T, bool>> expression)
    {
        return All().Where(expression).AsQueryable();
    }
}

最佳答案

此代码包括单个 session 范围内的查找和删除函数调用。正如我所想,问题代码中的问题是使用不同的代码。

public T RemoveById(int id)
{
    _transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
    T res=_session.Get<T>(id);
    _session.Delete(entity);
    _transaction.Commit(); 
}

(行动号召:)

RemoveById<MyEvent>(id)

关于mysql - 为什么我使用 nhibernate 的 asp.net-mvc 站点会停止更新和删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12552761/

相关文章:

wcf - NHibernate 取得结果前 20 名

mysql - mySql 中的两个 Dim (2-D) 数组或数据表?

asp.net-mvc - 如何在本地计算机上测试 twitter api

asp.net - 使用 asp.net mvc 和 Identity 的每个选项卡 session

c# - SQL Server 将 SP_EXECUTESQL 识别为对象而不是过程名称

.net - 在同一个解决方案中引用2个不同版本的log4net

asp.net-mvc - 将 NHibernate 应用程序转换为 Multi-Tenancy 的最佳实践?

MySQL查询速度慢是因为单独的索引?

mysql - 如果没有值,则计算行数返回 1

mysql - 在 MySQL 中创建 Unix 时间戳