如何在 FluentNHibernate 中使用外键映射 ValueObject 集合
我一直在寻找这样的例子,但它似乎很不常见。希望一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
但是你不能查询(列出所有)标签,因为它是一个值对象。
Try this:
but you cant query (list all) tags then because its a value object.