简体   繁体   English

在NHibernate中使用Merge()时出错:具有相同标识符的另一个对象

[英]Error when using Merge() in NHibernate: a different object with the same identifier

I'm using FluentNHibernate and when i make post got this error. 我正在使用FluentNHibernate,当我发帖时遇到此错误。

a different object with the same identifier value was already associated with the session 具有相同标识符值的另一个对象已与该会话相关联

That is my Controller: 那就是我的控制器:

private readonly ITicketAppService _ticketAppService;
public TicketController()
    {
        ITicketAppService ticketAppService = new TicketAppService(new TicketService(new TicketRepository(), new RomaneioCodRepository(), new ParamGerarMZRepository()));

    }

public ResponseApi Post(Ticket ticket)
     {
        var result = new ResponseApi();
        try{
        //my code goes here
        //my code goes here
        //my code goes here


        _ticketAppService.Add(ticket);
       _ticketAppService.Commit();

       result.DocsSaved.AddRange(ticket.ROMANEIOs);

       }
       catch (Exception ex)
       {
         _ticketAppService.RollBack();
         result.Errors.Add(new Erro(ex));
       }
       return result;
     }

And that is my Add on RepositoryBase: 那就是我在RepositoryBase上添加的内容:

public virtual void Add(TEntity obj)
        {
            try
            {
                BeginTrasaction();
                Db.Merge(obj);
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("Erro ao tentar adicionar alterações. Detalhes: {0}", ex);
                throw new Exception("Erro ao tentar adicionar registro.", ex);
            }
        }

All questions as i see sayed for use Merge() cause NHibernate try save one ID from user as is already on session, but i got the same error using Merge(). 我认为要使用Merge()的所有问题都会导致NHibernate尝试从会话中保存用户的一个ID,但是使用Merge()却遇到了相同的错误。 And i'm also using session-per-request. 而且我还在使用每个请求会话。 And i have tried make the same on this documentation . 我已经尝试过在此文档中做同样的事情。 Someone know's how can i fix that? 有人知道我该如何解决?

You are getting the same entity from multiple places using the same session, Merge method is used for attach an entity from different session. 您正在使用同一会话从多个位置获取同一实体, Merge方法用于附加来自不同会话的实体。

Read this Ayende's article . 阅读此Ayende的文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 n使具有相同标识符值的不同对象休眠 - nHibernate different object with the same identifier value Nhibernate“具有相同标识符值的不同对象”的问题 - Nhibernate 'different object with the same identifier value' issue NHibernate和AutoMapper玩得不好:“具有相同标识符值的不同对象已经存在” - NHibernate and AutoMapper not playing nice: “a different object with the same identifier value was already” 具有相同标识符值的不同 object 已与 session:0,实体相关联:AppCrashLogData - NHibernate - A different object with the same identifier value was already associated with the session: 0, of entity: AppCrashLogData - NHibernate Nhibernate与新对象图合并不存在具有给定标识符的行 - Nhibernate Merge with new object graph No row with the given identifier exists 具有相同标识符的不同对象已与会话错误相关联 - A different object with same identifier was already associated with the session error 级联错误:具有相同标识符值的另一个对象已与会话关联 - Error in Cascade : a different object with the same identifier value was already associated with the session 使用相同类型的对象(NHibernate,C#)访问不同的表 - Access different tables using the same type of object (NHibernate, C#) 使用标识符合并来自 2 个不同列表的结果 - Merge results from 2 different Lists using identifier 使用AutoMapper从POCO映射到NHibernate代理对象时出错 - Error when using AutoMapper to map from a POCO to an NHibernate proxy object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM