【问题标题】:Using Fluent NHibernate Auto Mapping to map IDs of type object from base Entity class使用 Fluent NHibernate Auto Mapping 从基实体类映射类型对象的 ID
【发布时间】:2009-07-06 19:37:12
【问题描述】:

在我现在正在进行的项目中,我们有如下所示的基本实体类:

public abstract class Entity<T> where T : Entity<T>
{
    public virtual object Id { get; protected set }    
    // Equals, GetHashCode overrides, etc...
}

从 Entity 继承的大多数类应该将 Id 映射到 SQL Server 数据库中的 int 列,但至少有一个需要映射到 long (bigint)。

是否可以创建 FluentNH 自动映射约定以默认将这些对象 ID 映射到 int?
然后我们可以使用另一个约定或 IAutoMappingOverride 来处理长 Id。

谢谢!

【问题讨论】:

    标签: nhibernate fluent-nhibernate nhibernate-mapping


    【解决方案1】:

    回答我自己的问题...有可能。

    你可以这样定义约定:

    internal class PrimaryKeyConvention : IIdConvention
    {
        public bool Accept(IIdentityPart id)
        {
            return true;
        }
    
        public void Apply(IIdentityPart id)
        {
            if (<ID should be long>)
                id.SetAttribute("type", "Int64");
            else
                id.SetAttribute("type", "Int32");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 2011-10-26
      相关资源
      最近更新 更多