如何在 FluentNHibernate 中使用外键映射 ValueObject 集合

发布于 2024-10-05 14:05:15 字数 1113 浏览 1 评论 0原文

我一直在寻找这样的例子,但它似乎很不常见。希望一些 NHibernate 大师会知道。

我有以下类,根​​据我对值对象的理解,它是一个值对象。假设每个用户都能够为任何问题分配一个或多个标签(想想 Stack Overflow)。标签不需要主键,但它们确实保存对用户和问题的引用,这与我看到的大多数 ValueObjects 示例不同。

public class Tag : ValueObject
{
     public virtual User User { get; set; }
     public virtual Question Question { get; set; }
     public virtual string TagName { get; set; }
}

public class User 
{
     public virtual IList<Tag> Tags { get; set; }
}

public class Question
{
     public virtual IList<Tag> Tags { get; set; }
}

无论如何,我收到以下错误:

{“实体‘标签’没有映射 Id。使用 Id 方法映射您的身份属性。例如:Id(x => x.Id)。”}

我有以下针对用户和问题的 Fluent NHibernate 映射:

public void Override(AutoMapping<XXX> mapping)
        {
            mapping.HasMany(x => x.Tags).Component(c =>
            {
                c.Map(x => x.TagName);
                c.Map(x => x.Question);
                c.Map(x => x.User);
            });
        }

一如既往,任何想法都非常感谢。

最新更新:好的,所以,也许这不是一个值对象。它不需要身份,但我想它也不是可以在多个地方使用的东西。有什么办法可以处理这个问题而不在我的对象上强制使用无用的 Id 字段?

I've been looking all over for an example of this, but it seems pretty uncommon. Hopefully some NHibernate guru will know.

I have the following class which, by my understanding of Value Objects, is a Value Object. Assume every user has the ability to assign one or more tags to any Question (think Stack Overflow). The Tags don't need a primary key, but they do hold references to the User and Question, unlike most of the examples of ValueObjects I see out there.

public class Tag : ValueObject
{
     public virtual User User { get; set; }
     public virtual Question Question { get; set; }
     public virtual string TagName { get; set; }
}

public class User 
{
     public virtual IList<Tag> Tags { get; set; }
}

public class Question
{
     public virtual IList<Tag> Tags { get; set; }
}

Anyway, I am getting the following error:

{"The entity 'Tag' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id)."}

I have the following Fluent NHibernate mapping for User and Question:

public void Override(AutoMapping<XXX> mapping)
        {
            mapping.HasMany(x => x.Tags).Component(c =>
            {
                c.Map(x => x.TagName);
                c.Map(x => x.Question);
                c.Map(x => x.User);
            });
        }

As always, any thought greatly appreciated.

Late Update: Okay, so, maybe this isn't a value object. It doesn't need an identity, but I guess it's not something that could be used in multiple places, either. Any way to handle this without forcing a useless Id field on my object?

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

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

发布评论

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

评论(1

转身泪倾城 2024-10-12 14:05:15

试试这个:

public void Override(AutoMapping<XXX> mapping)
    {
        mapping.HasMany(x => x.Tags).AsBag().Component(c =>
        {
            c.Map(x => x.TagName);
            c.References(x => x.Question);
            c.References(x => x.User);
        });
    }

但是你不能查询(列出所有)标签,因为它是一个值对象。

Try this:

public void Override(AutoMapping<XXX> mapping)
    {
        mapping.HasMany(x => x.Tags).AsBag().Component(c =>
        {
            c.Map(x => x.TagName);
            c.References(x => x.Question);
            c.References(x => x.User);
        });
    }

but you cant query (list all) tags then because its a value object.

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