【问题标题】:Map custom type in FluetnNhibernate在 FluetnNhibernate 中映射自定义类型
【发布时间】:2011-10-15 17:07:45
【问题描述】:

我有 2 节课。

public class Name
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class Person
{
    public virtual Name Name { get; set; }
    public virtual int Age { get; set; }
}

我想像这样将 Person 映射到数据库:

| First Name | LastName | Age |

我尝试为 Name 创建 IUserType 实现。但是这里

public SqlType[] SqlTypes
    {
        get { return new[] { new SqlType(DbType.String), new SqlType(DbType.String) }; }
    }

我有一个例外

property mapping has wrong number of columns

【问题讨论】:

    标签: nhibernate fluent-nhibernate mapping iusertype


    【解决方案1】:

    你真正需要的是一个组件:

    https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping#componentmap

    您的班级地图如下所示:

    public class NameComponent : ComponentMap<Name>
    {
        public NameComponent()
        {
            Map(x => x.FirstName);
            Map(x => x.LastName);
        }
    }
    
    public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Id(x => x.Id)...
            Map(x => x.Age);
            Component(x => x.Name);
        }
    }
    

    这会将 FirstName/LastName 作为两个单独的列映射到同一个 person 表。但是在你的 Person 上给你一个 Name 对象。


    如果您需要 AutoMap 组件,请查看此博客:

    http://jagregory.com/writings/fluent-nhibernate-auto-mapping-components/

    【讨论】:

      猜你喜欢
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 2011-01-24
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多