NHibernate 和 AutoMapper 表现不佳:“已经存在具有相同标识符值的不同对象”

发布于 2024-10-18 15:16:30 字数 646 浏览 0 评论 0原文

收到此错误: 具有相同标识符值的不同对象已经与会话关联:27,实体:xxx.Core.Event

基本上,我有从我的 poco 映射的视图模型,反之亦然。有问题的代码在这里:

Mapper.CreateMap<EventsAddEditViewModel, Event>();
        Event thisEvent = _eventRepository.GetById(viewModel.Id);
        thisEvent = Mapper.Map<EventsAddEditViewModel, Event>(viewModel);
        thisEvent.EventType = new EventType { Id = viewModel.EventTypeId };
        ValidationResult result = _eventService.Save(thisEvent);

基本上我从数据库加载事件,然后将视图模型映射到该事件并保存。否则,视图上未显示的字段(例如 dateCreated)将无法正确保存。

NHibernate 和 AutoMapper 有什么办法可以在这方面发挥出色吗?

我正在使用 OnePerRequestBehavior 作为我的会话提供程序。

Receiving this error:
a different object with the same identifier value was already associated with the session: 27, of entity: xxx.Core.Event

Basically, I have view models that are being mapped from my poco's and vice versa. The offending code is here:

Mapper.CreateMap<EventsAddEditViewModel, Event>();
        Event thisEvent = _eventRepository.GetById(viewModel.Id);
        thisEvent = Mapper.Map<EventsAddEditViewModel, Event>(viewModel);
        thisEvent.EventType = new EventType { Id = viewModel.EventTypeId };
        ValidationResult result = _eventService.Save(thisEvent);

Basically I'm loading the event from the database, and then mapping the view model to this event and saving. Otherwise there are fields that aren't shown on the view (dateCreated for example) which won't be saved properly.

Is there any way that NHibernate and AutoMapper can play nicely in this regard?

I'm using OnePerRequestBehavior for my session provider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦开始←不甜 2024-10-25 15:16:30

保存时将相同的对象归还给 NHibernate。

为此,我们使用 Mapper.Map() 的不同重载。此外,当编译器可以找出类型时,就不需要指定它们。

此外,GetById() 可能会返回 null。

Mapper.CreateMap<EventsAddEditViewModel, Event>();

Event thisEvent = _eventRepository.GetById( viewModel.Id );
if (thisEvent == null) {
    thisEvent = new Event();
}

Mapper.Map( viewModel, thisEvent );

thisEvent.EventType = new EventType { Id = viewModel.EventTypeId };
ValidationResult result = _eventService.Save( thisEvent );

Give NHibernate same object back when you save.

To do this we we use a different overload of Mapper.Map(). Also, when the compiler can figure out the types there is no need to specify them.

Also GetById() might return a null.

Mapper.CreateMap<EventsAddEditViewModel, Event>();

Event thisEvent = _eventRepository.GetById( viewModel.Id );
if (thisEvent == null) {
    thisEvent = new Event();
}

Mapper.Map( viewModel, thisEvent );

thisEvent.EventType = new EventType { Id = viewModel.EventTypeId };
ValidationResult result = _eventService.Save( thisEvent );
你的呼吸 2024-10-25 15:16:30

原因是该对象并不是您真正从 NHibernate 获得的对象。

要么使用该对象,要么如果您的真实代码与上面不同,那么这里是另一种方法...

在您的 NHibernate 代码中,您是否尝试了 session.Merge() 或 session.SaveOrUpdateCopy() 等而不是 session.Save()或会话.Update() ?

The reason is that the object is not the one you got from NHibernate really.

Either use that object, or if your real code is different than above then here is another approach...

In your NHibernate code, did you try session.Merge() or session.SaveOrUpdateCopy() or so instead of session.Save() or session.Update() ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文