Nhibernate类映射

时间:2010-10-01 17:36:30

标签: nhibernate fluent-nhibernate nhibernate-mapping

问题:我通过属性进行了以下nHibernate映射。

现在我想创建带有动态表前缀的T_lsDomains。 例如TBL_lsDomains或只是lsDomains。

我有什么方法可以用属性做到这一点? 既然它们是在编译时定义的,而且必须是常量吗?

有没有办法做到这一点?

或者可以流利的Nhibernate这样做吗?

using System;
using System.Collections.Generic;
using System.Text;

namespace nhDBapi.Tables
{

    [NHibernate.Mapping.Attributes.Class(Name = "nhDBapi.Tables.clsDomains, nhDBapi", Table = "T_lsDomains")]
    public class clsDomains
    {
        void clsDOmains()
        { 
        }


        [NHibernate.Mapping.Attributes.Id(Name = "DomainID", Column = "DM_DomainID", TypeType = typeof(string))]
        public string DomainID = "abc"; // nvarchar(100) NULL DEFAULT (''),

        [NHibernate.Mapping.Attributes.Property(Name = "DomainName", Column = "DM_DomainName", Type = "String", Length = 100)]
        string DomainName = ""; // nvarchar(100) NULL DEFAULT (''),

        [NHibernate.Mapping.Attributes.Property(Name = "Description", Column = "DM_Description", Type = "String", Length = 100)]
        string Description = ""; // nvarchar(100) NULL DEFAULT (''),
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用Fluent NHibernate convention。:

轻松实现此目的
public class TableNameConvention : IClassConvention
{
  public bool Accept(IClassMap classMap)
  {
    return true; // apply to all mappings
  }

  public void Apply(IClassMap classMap)
  {
    // will produce table names like: TBL_Customer, TBL_Product
    classMap.WithTable("TBL_" + classMap.EntityType.Name);
  }
}
相关问题