NHibernate:将多个列映射到一个集合中

发布于 2024-09-16 11:11:24 字数 386 浏览 2 评论 0原文

假设我有一个表:

 ID(pk) | HOME_EMAIL | WORK_EMAIL | OTHER_EMAIL
-------------------------------------------------

和 .NET 类,

class A {
    int id;
    List<MyEmail> emails;
}

class MyEmail {
    string email;
}

我想没有办法将这些(多个)列映射到 NHibernate 中的单个集合中,或者有吗? :)

到了这样的地步,我们不想再修改数据库模式了,所以我们不能对数据库做太多事情,如果这有帮助的话。

Suppose I have a table:

 ID(pk) | HOME_EMAIL | WORK_EMAIL | OTHER_EMAIL
-------------------------------------------------

and the .NET classes

class A {
    int id;
    List<MyEmail> emails;
}

class MyEmail {
    string email;
}

I suppose there's no way to map those (multiple) columns into a single collection in NHibernate, or is there? :)

It's come to a point that we'd rather not tinker with the database schema anymore so we can't do much with the database, if that helps.

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

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

发布评论

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

评论(3

落花浅忆 2024-09-23 11:11:24

我建议使用接口,这样你就可以做这样的事情

 interface IUser
{
    int Id {get; set;}
    IEnumerable<string> Emails {get;}
}

class MyUser : IUser
{
    public int Id {get; set;}
    public IEnumerable<string> Emails
    {
        get
        {
            return new [] { SomeEmail, SomeOtherEmail };
        }
    }

    public string SomeEmail { get; set; }
    public string SomeOtherEmail { get; set; }
}

你的应用程序可以期待一个 IUser,而不关心我们从哪里获得电子邮件列表。您可以在 NH 中映射 MyUser,而应用程序不(也不应该)关心实际的实现。

I would suggest working with Interfaces so you could do something like this

 interface IUser
{
    int Id {get; set;}
    IEnumerable<string> Emails {get;}
}

class MyUser : IUser
{
    public int Id {get; set;}
    public IEnumerable<string> Emails
    {
        get
        {
            return new [] { SomeEmail, SomeOtherEmail };
        }
    }

    public string SomeEmail { get; set; }
    public string SomeOtherEmail { get; set; }
}

Your application can expect an IUser and not care where we got the list of emails. You would map MyUser in NH, while the application does not (and should not) care about the actual implementation.

云雾 2024-09-23 11:11:24

如果它不必是集合,但可以是自定义类型,请说包含三个属性的 EmailAddresses

public class EmailAddresses
{
    public virtual string Home { get; set; }
    public virtual string Work { get; set; }
    public virtual string Other { get; set; }
}

您可以使用组件将三列映射到该对象的三个属性作为父级上的单个属性:

public class MyUser
{
    ...
    public virtual EmailAddresses { get; set; }
}

您可以使用 组件 或者如果您使用 Fluent NHibernateComponentMap 类映射(automapper 不能做组件)。

If it doesn't have to be a collection, but could be a custom type instead, say EmailAddresses which contains three properties:

public class EmailAddresses
{
    public virtual string Home { get; set; }
    public virtual string Work { get; set; }
    public virtual string Other { get; set; }
}

You could use a component to map the three columns into the three properties of this object as a single property on the parent:

public class MyUser
{
    ...
    public virtual EmailAddresses { get; set; }
}

You can map these in NHibernate using components or if you're using Fluent NHibernate with the ComponentMap<T> classmap (automapper can't do components).

往日 2024-09-23 11:11:24

有一个功能非常接近您想要的功能,

文档位于 http://nhibernate.info/doc/nh/en/index.html#components-dynamic 应该可以帮助您入门。

There is a feature that's very close to what you want, <dynamic-component>

The documentation at http://nhibernate.info/doc/nh/en/index.html#components-dynamic should get you started.

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