Fluent NHibernate entity HasMany collections of different subclass types

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 08:26:52

问题


So everything is working well with the basic discriminator mapping. I can interact directly with entities A and B without any problems.

public class BaseType {}
public class EntityA : BaseType {}
public class EntityB : BaseType {}

This maps without drama in the BaseType mapping as

DiscriminateSubClassesOnColumn<string>("Type")
               .SubClass<BaseType>("A", m => { })
               .SubClass<BaseType>("B", m => { });

The problem occurs when: in an aggregate we want to map collections to each subclass

Using mapping like below

public class AggregateMap: BaseMap<Aggregate>
{
        public AggregateMap()
        {
                HasMany<EntityA>(x => x.ACollection).AsSet().Cascade.All();
                HasMany<EntityB>(x => x.BCollection).AsSet().Cascade.All();            
        }
}

These obviously arent full mappings but are the minimum amount to descibe what I am attempting. Items added to ACollection and BCollection are persisted correctly through the cascading when Aggregate is saved. However, when aggregate is retrieved there is confusion on the type discrimination.

I have run through so many different possible solutions I no longer know what didn't work. I feel that I shouldn't have to provide a where clause on the collections but things just aren't working for me.

Any clues would be appreciated.


回答1:


You mapping looks odd, in particular I think it should look more like this

DiscriminateSubClassesOnColumn<string>("Type")
               .SubClass<EntityA>("A", m => { })
               .SubClass<EntityB>("B", m => { });

Having said that it seems that method is depreciated and you should instead define the following (taken from Automapping Subclasses:

public class ParentMap : ClassMap<Parent>
{
  public ParentMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);

    DiscriminateSubClassesOnColumn("type");
  }
}

public class ChildMap : SubclassMap<Child>
{
  public ChildMap()
  {
    Map(x => x.AnotherProperty);
  }
} 

Not sure this will fix it though, I am yet to encounter your scenario.

Edit: The issue is also raised here, sounding more like a bug to me



来源:https://stackoverflow.com/questions/985213/fluent-nhibernate-entity-hasmany-collections-of-different-subclass-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!