为什么我的asp.net MVC的使用NHibernate简单地停止做更新和删除网站? [英] Why would my asp.net-mvc site using nhibernate simply stop doing updates and deletes?

查看:112
本文介绍了为什么我的asp.net MVC的使用NHibernate简单地停止做更新和删除网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用NHibernate的一个MySQL数据库的接口非常简单的CRUD asp.net MVC的网站。我使用的UnitOfWork和信息库模式。 (通过的NuGet)升级到MVC 4和最新的NHibernate和MySQL的版本之后,我突然看到更新和删除已经停止工作一个奇怪的问题。

下面是一个例子删除我的控制器code,它停止工作:

 公众的ActionResult删除(INT ID)
    {
        MyEvent C = _eventRepository.FindBy(ID);        _unitOfWork.Begin();
        _eventRepository.Delete(C);
        _unitOfWork.End();        返回RedirectToAction(「指数」);
    }

其中的UnitOfWork code是这样的:

 公开的UnitOfWork(ISessionFactory SessionFactory的)
    {
        _sessionFactory = SessionFactory的;
        会话= _sessionFactory.OpenSession();
        Session.FlushMode = FlushMode.Auto;
    }   公共无效结束()
    {
        承诺();
        如果(Session.IsOpen)
        {
            Session.Close();
        }
    }    公共无效的commit()
    {
        如果(!_transaction.IsActive)
        {
            抛出新的InvalidOperationException异常(不活跃TRANSATION);
        }
        _transaction.Commit();
    }    公共无效开始()
    {
        _transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
    }

我测试补充说,工作的罚款(新行中数据表显示出来)一个新的项目,但是当我测试或者更新或删除时,code运行正常(我没有得到在$ C $任何异常C)但字段不被当我做一个更新和更新,当我运行删除code未删除的记录。

因此​​,要回顾一下,从MySQL数据库中读取数据正常工作,这样做增加了工作正常,但更新和删除已停止所有表(这之前没有工作)工作。我做了一个测试用做蟾蜍为MySQL常规的SQL和正常工作(使用我使用在我的code连接相同的登录凭据)

要帮助调试多一点,我就开始了NHibernate的探查,这是我看到的删除或更新条目:

这是我看到加载定期阅读页面:

不知道这是有帮助的解释这个问题,但我想它不能伤害增加的截图。

什么可能会发生任何建议。难道这是一种权利的问题(对某些软件库中的bug?)。同样,高于这个code $ P $提到pviously肯定的工作。

下面是我的Ninject的Ioc code:

 字符串的ConnectionString = ConfigurationManager.ConnectionStrings [LocalMySqlServer]的ConnectionString。        VAR帮手=新NHibernateHelper(的connectionString);
        绑定< ISessionFactory>()ToConstant(helper.SessionFactory)。
            .InSingletonScope();        绑定&所述; IUnitOfWork方式>()到<的UnitOfWork>();        VAR sessionProvider =新SessionProvider();
        绑定<&ISession的GT;()ToProvider(sessionProvider)。        VAR的UnitOfWork =新的UnitOfWork(helper.SessionFactory);        绑定(typeof运算(IIntKeyedRepository<>))。为了(typeof运算(库<>));
    }

和这里是我的unitofwork.cs code:

 公共类的UnitOfWork:IUnitOfWork
{
    私人只读ISessionFactory _sessionFactory;
    私人ITransaction _transaction;    公众的ISession会议{搞定;私人集; }    公众的UnitOfWork(ISessionFactory SessionFactory的)
    {
        _sessionFactory = SessionFactory的;
        会话= _sessionFactory.OpenSession();
        Session.FlushMode = FlushMode.Auto;
    }    公共无效结束()
    {
        承诺();
        如果(Session.IsOpen)
        {
            Session.Close();
        }
    }    公共无效开始()
    {
        _transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
    }    公共无效的Dispose()
    {
        如果(Session.IsOpen)
        {
            Session.Close();
        }
    }    公共无效的commit()
    {
        如果(!_transaction.IsActive)
        {
            抛出新的InvalidOperationException异常(不活跃TRANSATION);
        }
        _transaction.Commit();
    }    公共无效回滚()
    {
        如果(_transaction.IsActive)
        {
            _transaction.Rollback();
        }
    }
}

和这里是我的仓库code:

 公共类资源库< T> :IIntKeyedRepository< T>其中T:类
{
    私人只读的Isession _session;
    私人ITransaction _trans;    公共ŧFindBy(INT ID)
    {
        返回_session.Get< T>(ID);
    }    公共库(ISession的会话)
    {
        _session =会议;
    }    公共BOOL添加(T实体)
    {
        _session.Save(实体);
        返回true;
    }    公共BOOL添加(IEnumerable的< T>的项目)
    {
        的foreach(在项目牛逼项)
        {
            _session.Save(项目);
        }
        返回true;
    }    公共BOOL更新(T实体)
    {
        _session.Update(实体);
        返回true;
    }    公共BOOL删除(T实体)
    {
        _session.Delete(实体);
        返回true;
    }    公共BOOL删除(IEnumerable的< T>实体)
    {
        的foreach(在实体T实体)
        {
            _session.Delete(实体);
        }
        返回true;
    }    #endregion    #地区IIntKeyedRepository< T>会员    公共ŧFindBy(INT ID)
    {
        返回_session.Get< T>(ID);
    }    #endregion    #地区IReadOnlyRepository< T>会员    公众的IQueryable< T>所有()
    {
        返回_session.Query< T>();
    }    公共ŧFindBy(前pression<&Func键LT; T,BOOL>>前pression)
    {
        返回FilterBy(如pression)。单();
    }    公众的IQueryable< T> FilterBy(前pression<&Func键LT; T,BOOL>>前pression)
    {
        返回所有(),其中(EX pression).AsQueryable()。
    }
}


解决方案

这code,包括查找和删除功能调用单个会话范围。因为我认为,从问题code问题是使用不同的。

 公共牛逼RemoveById(INT ID)
{
    _transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
    ŧ解析度= _session.Get< T>(ID);
    _session.Delete(实体);
    _transaction.Commit();
}

(从行动呼吁:)

  RemoveById< MyEvent>(ID)

I have a very simple CRUD asp.net-mvc site that uses nhibernate to interface with a mySQL db. I am using the UnitOfWork and Repository patterns. After upgrading to MVC 4 and the latest nhibernate and mySQL versions (via nuget) I am suddenly seeing a weird issue where updates and deletes have stopped working.

Here is an example Delete code in my controller that stopped working:

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

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

        return RedirectToAction("Index");
    }

where the UnitOfWork code looks like this:

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

I tested adding a new item that worked fine (new row shows up in DB table) but when I test either Updates or Deletes, the code runs fine (I don't get any exceptions in the code) but the fields aren't being updated when i do an Update and the record isn't deleted when I run the delete code.

So to recap, reading data from mySQL db works fine, doing adds works fine but updates and deletes have stopped working for all tables (which did work before). I did a test doing regular SQL using Toad for MySQL and that worked fine (using the same login credentials that i am using to connect in my code)

To help debug a bit more, I started up nhibernate profiler and this is what i see for a delete or update entry:

and this is what i see loading a regular read page:

not sure if that is helpful in explaining the issue but i figured it couldn't hurt to add the screenshots.

Any suggestions on what could be happening. Could this be an entitlement issue (versus some software library bug?). Again, as mentioned above this code previously definitely worked.

Here is my Ninject Ioc Code:

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

and here is my unitofwork.cs code:

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

and here is my repository code:

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

解决方案

This code includes Find and Delete functions calls in scope of a single session. As I think, problem in code from question is using different ones.

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

(call from action:)

RemoveById<MyEvent>(id)

这篇关于为什么我的asp.net MVC的使用NHibernate简单地停止做更新和删除网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆