流利Nhibernate如何在SubclassMap中指定Id

流利Nhibernate如何在SubclassMap中指定Id

本文介绍了流利Nhibernate如何在SubclassMap中指定Id()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$ p我正在调整流利的NHibernate到我们现有的遗留应用程序,并试图确定如何使用ClassMap和SubclassMap的实体层次结构显示。

$ b $不包含数据库id列
公共字符串CommonDbCol1 {get;组; }
public string CommonDbCol2 {get;组; }
// ...
}

public class Entity1:BaseObject
{
public int Entity1Id {get;组; }
//其他实体1属性
}

public class实体2:BaseObject
{
public int Entity2Id {get;组; }
//其他实体2属性
}

Entity1和Entity2是每个表唯一命名的。 BaseObject包含所有实体共有的列。我不使用AutoMapping,并认为我可以在BaseObject上使用ClassMap,然后像这样在每个实体上使用SubclassMap:

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

问题是,Id()没有为SubclassMap定义。那么,如何在每个Entity1Map,Entity2Map,...(我们有100多个实体类都从BaseObject继承)中指定实体特定的Id是什么?



在此先感谢您的任何见解!

解决方案

在Fluent NHibernate或NHibernate中,这是不可能的。你真的想要你的类被映射为子类,或者你只是想让它们共享通用映射吗?如果你真的想要子类,那么你将需要让他们分享标识列,没有其他方式;如果你不想要实际的子类,创建一个抽象的 ClassMap< T>其中T:BaseObject ,并映射其中的常用属性。



类似于:

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


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 advance 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);
  }
}

这篇关于流利Nhibernate如何在SubclassMap中指定Id()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 00:26