如何处理更新实体.NHibernate + ASP.NET MVC

lex*_*eme 10 c# nhibernate asp.net-mvc orm exception

我无法更新以前创建的实体.我收到一条StaleObjectException带有消息的异常:

Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Project.DomainLayer.Entities.Employee#00000000-0000-0000-0000-000000000000]

我不与任何人分享更新过程.有什么问题?

数据访问/ DI

public class DataAccessModule : Ninject.Modules.NinjectModule
{
    public override void Load()
    {
        this.Bind<ISessionFactory>()
            .ToMethod(c => new Configuration().Configure().BuildSessionFactory())
            .InSingletonScope();

        this.Bind<ISession>()
            .ToMethod(ctx => ctx.Kernel.TryGet<ISessionFactory>().OpenSession())
            .InRequestScope();

        this.Bind(typeof(IRepository<>)).To(typeof(Repository<>))
            .InRequestScope();
    }
}
Run Code Online (Sandbox Code Playgroud)

数据访问/映射

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Project.DomainLayer"   namespace="Project.DomainLayer.Entities">
<class name="Employee" optimistic-lock="version">
    <id name="ID" column="EmployeeID" unsaved-value="00000000-0000-0000-0000-000000000000">
        <generator class="guid.comb" />
    </id>
    <version name="Version" type="Int32" column="Version" />
    <!-- properties -->
    <property name="EmployeeNumber" />
    <!-- ... -->
    <property name="PassportRegistredOn" not-null="true" />
    <!-- sets -->
    <set name="AttachedInformation" cascade="all">
        <key column="EmployeeID" />
        <element column="Attachment" />
    </set>
    <set name="TravelVouchers" cascade="all">
        <key column="EmployeeID" />
        <one-to-many class="TravelVoucher" />
    </set>
  </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

数据访问/存储库

public class Repository<T> : IRepository<T> where T : AbstractEntity<T>, IAggregateRoot
{
    private ISession session;

    public Repository(ISession session)
    {
        this.session = session;
    }

    // other methods are omitted

    public void Update(T entity)
    {            
        using(var transaction = this.session.BeginTransaction())
        {
            this.session.Update(entity);
            transaction.Commit();
        }
    }
    public void Update(Guid id)
    {            
        using(var transaction = this.session.BeginTransaction())
        {
            this.session.Update(this.session.Load<T>(id));
            transaction.Commit();
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

控制器内部

public class EmployeeController : Controller
{
    private IRepository<Employee> repository;

    public EmployeeController(IRepository<Employee> repository)
    {
        this.repository = repository;
    }        
    public ActionResult Edit(Guid id)
    {
        var e = repository.Load(id);
        return View(e);
    }
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(Employee employee)
    {
        if(ModelState.IsValid)
        {
            repository.Update(employee);
            return RedirectToAction("Deatils", "Employee", new { id = employee.ID });
        }
        else
        {
            return View(employee);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何更新我的实体?谢谢!

编辑

所以我加入unsaved-value="{Guid.Empty goes here}"了我的标记.而且我试着做下一件事:

public void Update(T entity)
{
    using(var transaction = this.session.BeginTransaction())
    {
        try
        {
            this.session.Update(entity);
            transaction.Commit();
        }
        catch(StaleObjectStateException ex)
        {
            try
            {
                session.Merge(entity);
                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我同样的效果..我的意思是transaction.Commit();Merge给出相同的例外之后.

另外,我想知道我应该使用隐藏的输入,IDEdit视图上公开实体吗?

编辑

所以实体真的脱离了.当它传递给控制器​​时ID等于Guid.Empty.我该如何处理,MergeReattach

New*_*bie 10

根据您的代码模式,您可以遇到两种情况.

  1. 您可以从数据库中检索对象,然后可以对检索到的对象ISession.Get()进行更改/更新.要使此更改生效,您需要做的就是刷新会话或提交事务,因为Nhibernate会自动跟踪所有更改.

  2. 您有一个瞬态实例,一个与ISession上下文无关的对象,您要从中更新.在这种情况下,根据我的经验,最佳实践是ISession.Get()对象并对刚刚检索的对象进行相应的更改.(通常您的视图模型也与您的域模型不同,不要混合使用)此模式如下所示.它一直有效.确保你也使用ISession.SaveOrUpdate().

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Employee employee)
{
    if(ModelState.IsValid)
    {
        var persistentEmployee = repository.Get(employee.Id);
        if(  persistentEmployee == null){
            throw new Exception(String.Format("Employee with Id: {0} does not exist.", employee.Id));
        }
        persistentEmployee.Name = employee.Name;
        persistentEmployee.PhoneNumber = employee.PhoneNumber;
        //and so on
        repository.Update(persistentEmployee);
        return RedirectToAction("Deatils", "Employee", new { id = employee.ID });
    }
    else
    {
        return View(employee);
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,请注意您的控制器可能是基于每个请求实例化的,因此,您的生命周期ISession不会跨越您对控制器中不同方法的多次调用.换句话说,每种方法几乎总是在新的ISession(工作单元)环境中工作.