简体   繁体   English

如何正确 map 并使用 fluent-nhibernate 更新自引用实体

[英]How to correctly map & update a self referencing entity with fluent-nhibernate

I'm trying to create an entity which currently owns a List of the same entity, this link exists in another table.我正在尝试创建一个当前拥有同一实体列表的实体,此链接存在于另一个表中。

This is then exposed as an API.然后将其公开为 API。 I'm able to fetch data, but everytime I try to update the collection, it results in an error: a collection with cascade= all-delete-orphan was no longer referenced by the owning entity instance我能够获取数据,但每次我尝试更新集合时,都会导致错误: a collection with cascade= all-delete-orphan was no longer referenced by the owning entity instance

public class MyEntity
{
   public virtual int Id { get; set; }
   public virtual ISet<MyEntity> LinkedEntities { get; set; }
}

I'm using fluent nhibernate automapping, and so, overriding the default mapping.我正在使用流利的 nhibernate 自动映射,因此覆盖了默认映射。

var fluentConfigurationCampaign = Fluently.Configure()
.CurrentSessionContext("mySession")
.Database(
          MySQLConfiguration.Standard.ConnectionString(MyConnectionString)
        )
        .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<MyEntity>(cfg)
.Conventions.Setup(c => c.Add<CustomForeignKeyConvention>())
.Override<MyEntity>(map => map.HasManyToMany(i => i.LinkedEntities)
.ParentKeyColumn("EntitySelf")
.ChildKeyColumn("EntityOther")
.Table("Linked").Cascade.AllDeleteOrphan())

The linked table, as suggested by the mapping contains only 3 properties.映射建议的链接表仅包含 3 个属性。

A self generated ID自己生成的 ID

A foreignKey EntitySelf linking to the Entity table链接到 Entity 表的 foreignKey EntitySelf

A foreignKey EntityOther linking to the Entity table.链接到 Entity 表的 foreignKey EntityOther。

When updating an entity (through a controller), I'm getting an EntityContract which is mapped through automapper to an Entity.更新实体(通过控制器)时,我得到一个 EntityContract,它通过自动映射器映射到一个实体。

I then do:然后我做:

_session.Merge(myEntityFromController);
var oldEntity = _session.Get<T>(myEntityFromController.Id);
_session.Update(oldEntity);
_session.Flush();

Which is working fine for all my other entities.这对我所有其他实体都很好。 But, trying to change the List of LinkedEntities (either by removing some, or adding, or even not touching it), I got the error: a collection with cascade= all-delete-orphan was no longer referenced by the owning entity instance但是,尝试更改 LinkedEntities 列表(通过删除一些,或添加,甚至不触摸它),我得到了错误: a collection with cascade= all-delete-orphan was no longer referenced by the owning entity instance

Any help would be appreciated as I'm stuck on this issue.任何帮助将不胜感激,因为我被困在这个问题上。 Thanks.谢谢。

It seems my issue was related to the .Cascade.AllDeleteOrphan() part of the mapping.看来我的问题与映射的.Cascade.AllDeleteOrphan()部分有关。 I thought it would reference to my Linked table, but instead, it seems it was trying to act on the MyEntity linked, hence producing my error.我以为它会引用我的Linked表,但相反,它似乎试图对链接的MyEntity采取行动,因此产生了我的错误。

Removing it made everything works with no other tweak needed.删除它使一切正常,无需其他调整。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM