开发者

Fluent Nhibernate How to specify Id() in SubclassMap

I'm in the process of adapting Fluent NHibernate to our existing legacy app and am trying to determine how to use ClassMap and SubclassMap for the entity hierarchy shown.

// BaseObject contains database columns common to every table
public class BaseObject
{
    // does NOT contain database id column
    public string CommonDbCol1 { get; set; }
    public string CommonDbCol2 { get; set; }
    // ...
}

public class Entity1 : BaseObject
{
    public int Entity1Id { get; set; }
    // other Entity1 properties
}

public class Entity2 : BaseObject
{
    public int Entity2Id { get; set; }
    // other Entity2 properties
}

The identity columns for Entity1 and Entity2 are uniquely named per table. BaseObject contains columns that are common to all of our entities. I am not using AutoMapping, and thought I could use ClassMap on the BaseObject, and then use SubclassMap on each Entity like this:

public class Entity1Map : SubclassMap<Entity1>
{
    public Entity1Map()
    {
        Id(x => x.Entity1Id);
        // ...
    }
}

Problem is, Id() is not defined for SubclassMap. So, how do I specify within each Entity1Map, Entity2Map, ... (we have 100+ entity classes all inheriting from BaseObject) what the entity-specific Id is?

Thanks in advanc开发者_如何学JAVAe for any insight!


It's not possible to do that in either Fluent NHibernate or NHibernate. Do you actualy want your classes to be mapped as subclasses, or do you just want them to share the common mappings? If you truly want subclasses, then you're going to need to have them share the identity column, no other way around it; if you don't want actual subclasses, create an abstract ClassMap<T> where T : BaseObject and map the common properties in there.

Something like:

public abstract class BaseObjectMap<T> : ClassMap<T> where T : BaseObject
{
  public BaseObjectMap()
  {
    Map(x => x.CommonProperty1);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜