在Fluent Nhibernate中映射自定义类型

时间:2011-07-29 06:28:40

标签: nhibernate fluent-nhibernate mapping iusertype

我有两节课。

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

1 个答案:

答案 0 :(得分:3)

你实际要求的是一个组件:

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上给你一个Name对象。


如果您需要自动映射组件,请查看此博客:

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

相关问题