为什么我的asp.net-mvc网站使用nhibernate只是停止进行更新和删除?

时间:2012-09-23 13:32:18

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

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

以下是我的控制器中停止工作的示例删除代码:

    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);
    }

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

所以回顾一下,从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();
    }
}

1 个答案:

答案 0 :(得分:1)

此代码包括单个会话范围内的查找和删除函数调用。我认为,问题代码中的问题是使用不同的问题。

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

(从行动致电:)

RemoveById<MyEvent>(id)