Fluent NHibernate Polymorphic Mapping Challenges

纵饮孤独 提交于 2019-12-11 08:17:24

问题


I am having some problems mapping the following scenario in Fluent Nhibernate using Table Per Concrete class:

Let's say I have the following class definitions:

public class Reading { .... }

public class CarReading : Reading { .... }

public class TruckReading : Reading { .... }

public class Alert 
{
    ....
    public virtual Reading AReading { get; set; }
}

So my question is how to create the mapping class for Alert, if it has a one to one relationship with reading class (could be either truck reading or car reading) and instruct nhibernate to know which table to load the data from (TruckReading table or CarReading Table)

public class AlertMap : ClassMap<Alert>
{
    ....
    HasOne(x => x.AReading);
}

If anyone could point me in the right direction that would be much appreciated.

Thanks.


回答1:


public class AlertMap : ClassMap<Alert>
{
    ....
    ReferenceAny(x => x.AReading)
        .EntityIdentifierColumn("readingid")
        .EntityTypeColumn("readingtype")
        .IdentityType<int>()
        .AddMetaValue<CarReading>("car")
        .AddMetaValue<TruckReading>("truck");
}


来源:https://stackoverflow.com/questions/12812042/fluent-nhibernate-polymorphic-mapping-challenges

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