如何在 FluentNhibernate 中从具有 2 列 CompositeId 的类引用具有 2 列 CompositeId 的类?
我正在尝试
public class BaseMap<T> : ClassMap<T>
为我的应用程序创建一个。它应该支持本地化实体,它们的主键必须是 Id 和 Id。语言:
CompositeId()
.KeyProperty(x => x.Id)
.KeyProperty(x => ((ILocalizedEntity)x).Language);
我想要的另一件事是一个本地化类能够引用另一个本地化类。
我已经完成了以下操作(经过大量研究,这就是我所拥有的全部):
public new ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> memberExpression) where TOther : BaseEntity
{
if (typeof(ILocalizedEntity).IsAssignableFrom(typeof(TOther)))
{
return base.References(memberExpression)
.Columns("CategoryId")
.Columns("Language");
}
return base.References(memberExpression);
}
这在尝试插入时给了我 IndexOutOfRange 异常,原因是“Language”属性被映射了两次,但只有 1 个语言列在数据库中,所以我这样做了:
public new ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> memberExpression) where TOther : BaseEntity
{
if (typeof(ILocalizedEntity).IsAssignableFrom(typeof(TOther)))
{
return base.References(memberExpression)
.Columns("CategoryId")
.Columns("Language")
.Not.Update()
.Not.Insert();
}
return base.References(memberExpression);
}
这解决了 IndexOutOfRange 问题,但没有完成我想要的。因为它总是将 NULL 插入到“CategoryId”列,因为我指定了 Not.Insert() 和 Not.Update(),所以它不会!
现在,我希望它映射“CategoryId”而不是“Language”,因为它已经映射了,因为它是 ComposideId(主键)的一部分。
所以我尝试了一下:
public new ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> memberExpression) where TOther : BaseEntity
{
if (typeof(ILocalizedEntity).IsAssignableFrom(typeof(TOther)))
{
return base.References(memberExpression)
.Columns("CategoryId")
.Nullable()
.Columns("Language")
.Not.Update()
.Not.Insert();
}
return base.References(memberExpression);
}
希望它不会只插入或更新“Language”而不是“CategoryId” - 但也没有运气。
我还尝试在 PK 中首先使用语言:
CompositeId()
.KeyProperty(x => ((ILocalizedEntity)x).Language)
.KeyProperty(x => x.Id);
并将参考更改为:
return base.References(memberExpression)
.Columns("Language")
.Not.Update()
.Not.Insert()
.Columns("CategoryId")
.Nullable();
但“Not.Insert()”和“Not.Update()”仍然影响“语言”和“语言”。 “类别 ID”。
我尝试在“References”调用之前映射“CategoryId”,如下所示:
Map(memberExpression, "Language");
return base.References(memberExpression)
.......
但失败了。
任何人都知道,当其中 1 列为主键和外键共用时,如何从具有 2 列 CompositeId 的类引用具有 2 列 CompositeId 的类?
I'm trying to create a
public class BaseMap<T> : ClassMap<T>
for my application. It should support localized entities, which their primary key must be both Id & Language:
CompositeId()
.KeyProperty(x => x.Id)
.KeyProperty(x => ((ILocalizedEntity)x).Language);
Another thing I want is one localized class to be able to reference another localized class.
I've done the following (After a lot of research, This is all I have):
public new ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> memberExpression) where TOther : BaseEntity
{
if (typeof(ILocalizedEntity).IsAssignableFrom(typeof(TOther)))
{
return base.References(memberExpression)
.Columns("CategoryId")
.Columns("Language");
}
return base.References(memberExpression);
}
This gave me IndexOutOfRange exception when trying to insert, the reason was that the "Language" property was mapped twice, but there was only 1 language column in the DB, so I did this:
public new ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> memberExpression) where TOther : BaseEntity
{
if (typeof(ILocalizedEntity).IsAssignableFrom(typeof(TOther)))
{
return base.References(memberExpression)
.Columns("CategoryId")
.Columns("Language")
.Not.Update()
.Not.Insert();
}
return base.References(memberExpression);
}
Which resolved the IndexOutOfRange problem, but didn't accomplished what I want. because it always inserts NULL to the column "CategoryId", because I specified Not.Insert() and Not.Update(), so it doesn't!
Now, I am in a situation I want it to map "CategoryId" but not "Language" because it's already mapped, as it's part of the ComposideId (primary key).
So I gave a try to this:
public new ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> memberExpression) where TOther : BaseEntity
{
if (typeof(ILocalizedEntity).IsAssignableFrom(typeof(TOther)))
{
return base.References(memberExpression)
.Columns("CategoryId")
.Nullable()
.Columns("Language")
.Not.Update()
.Not.Insert();
}
return base.References(memberExpression);
}
Whishing it will not insert or update only "Language" and not "CategoryId" - but no luck there either.
I've also tried to make language first in the PK:
CompositeId()
.KeyProperty(x => ((ILocalizedEntity)x).Language)
.KeyProperty(x => x.Id);
And changed the Reference to:
return base.References(memberExpression)
.Columns("Language")
.Not.Update()
.Not.Insert()
.Columns("CategoryId")
.Nullable();
But still the "Not.Insert()" and "Not.Update()" is affecting both "Language" & "CategoryId".
I tried mapping the "CategoryId" before the "References" call, like this:
Map(memberExpression, "Language");
return base.References(memberExpression)
.......
But it failed.
Anyone has any idea, how to reference a class with 2 columns CompositeId from a class with 2 columns CompositeId when 1 of the columns is common to the primary key and the foreign key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到了类似的问题,复合ID的一部分是参考,我无法通过谷歌解决它,所以也无法自己尝试两周,因此不喜欢他们。
在某些情况下,
CompositeId
有它的位置,但在您描述的情况下很可能没有。使用简单的字典和方便的属性来获取实际语言的值要简单得多,并且避免重复对每种语言都相同的所有其他属性。i had a similar problem with a part of the compositeid being a reference and i couldn't resolve it either with google, SO nor trying on my own for 2 weeks, hence the dislike against them.
There are cases where
CompositeId
has its place but in the case you are describing most probably not. A simple dictionary and convenience properties to get the value for the actual language is much simpler and avoids duplicating all other properties which are the same for every language.