在 FluetnNhibernate 中映射自定义类型

发布于 2024-11-27 01:42:42 字数 613 浏览 2 评论 0原文

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

I have 2 classes.

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; }
}

I want to map Person to database like this:

| First Name | LastName | Age |

I tried to create IUserType implementation for Name. But here

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

I have an exception

property mapping has wrong number of columns

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬﹪忆之殇 2024-12-04 01:42:42

您实际上要求的是一个组件:

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 对象。


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

https://web.archive.org/web/20190212181857/http://www.jagregory.com:80/writings/ Fluent-nhibernate-auto-mapping-components/

What you're actually asking for is a Component:

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

Your class maps would look like:

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);
    }
}

That will map the FirstName/LastName to the same person table as two separate columns. But give you a single Name object on your Person.


If you need to AutoMap the component, take a look at this blog:

https://web.archive.org/web/20190212181857/http://www.jagregory.com:80/writings/fluent-nhibernate-auto-mapping-components/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文