2

我有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
4

1 回答 1

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 表。但是在你的 Person 上给你一个 Name 对象。


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

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

于 2011-07-29T06:47:51.123 回答