【问题标题】:Is it possible in nHibernate to set a property NOT to map to a column in a table while other properties of the class does是否可以在 nHibernate 中设置一个属性而不是映射到表中的列,而该类的其他属性可以
【发布时间】:2012-01-29 14:17:21
【问题描述】:

我有一个类和所有属性,但一个映射到相应的表列。现在,当 nhibernate 创建类的新实例时,我想将剩下的一个属性设置为某个值。

例如。

    --only two columns

    CREATE TABLE [dbo].[Person](
        [key] [int] NOT NULL,
        [Name] [nvarchar](50) NULL) 
    --..........


    public class Entity
    {
    public int key {get;set;}
    public string EntityType {get;set;}
    }

    public class Person : Entity
    {
    public string Name {get;set;} 
    }


and i have maps
            Map(Person => Person.key).Not.Nullable();
            Map(Person => Person.Name).Not.Nullable();  
            // i don't want a map for EntityType 

这里我想要一个 Person 实例,EntityType 应该始终设置为自定义字符串“Person”。其他属性通常映射到 Person Table 的相应列。

这可能吗?

【问题讨论】:

  • 从继承类的构造函数中设置EntityType属性怎么样?或者也许只是 public string EntityType { get { return this.GetType().Name; } }

标签: nhibernate orm mapping subclassing


【解决方案1】:

只需使用您想要的值初始化Person 构造函数中的EntityType 属性。 你不需要映射它。

public class Person : Entity
{
    public Person()
    {
        EntityType = "Person";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多