【问题标题】:Nhibernate class mappingsNhibernate 类映射
【发布时间】:2010-10-03 04:42:10
【问题描述】:

问题:我通过属性有以下 nHibernate 映射。

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

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

有什么办法吗?

或者fluentNhibernate可以做到这一点吗?

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 (''),
    }
}

【问题讨论】:

  • 请指定 T_lsDomains 指的是什么,以及您要创建的具体内容
  • T_lsDomains 是一个数据库表。我想创建一个动态名称的表。

标签: nhibernate fluent-nhibernate nhibernate-mapping


【解决方案1】:

您可以使用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);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2010-11-05
    相关资源
    最近更新 更多