流利的NHibernate多态映射挑战 [英] Fluent NHibernate Polymorphic Mapping Challenges

查看:129
本文介绍了流利的NHibernate多态映射挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我有以下类定义:

  public class Reading {....} 

public class CarReading:Reading {....}

public class TruckReading:Reading {....}

public class Alert
{
....
public virtual Reading AReading {get ;组;所以我的问题是如何创建Alert的映射类,如果它与阅读课有一对一的关系(可以是卡车阅读或汽车阅读),并指示nhibernate知道从哪个表加载数据(TruckReading表或CarReading表)

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



$ b $ p
$ b

如果有人能指出我正确的方向, / p>

谢谢。

解决方案

  public class AlertMap:ClassMap< Alert> 

...
ReferenceAny(x => x.AReading)
.EntityIdentifierColumn(readingid)
.EntityTypeColumn(readingtype)$ (car)
.AddMetaValue< TruckReading>(truck); b $ b.IdentityType< int>
}


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.

解决方案

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

这篇关于流利的NHibernate多态映射挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆