如何使用 FluentNHibernate 映射具有复杂键类型 (CultureInfo) 的字典
我有使用 Fluent NHibernate 映射的字典。该字典有一个复杂的键类型CultureInfo
。我的数据库无法存储该类型,因此我想使用它的字符串表示形式。
在字典映射以外的映射中,我可以使用用户类型约定成功映射 CultureInfo
属性。现在我想知道如何进行双标映射。
这是包含字典的实体:
public class MultilingualPhrase : Entity
{
private IDictionary<CultureInfo, string> languageValues;
public virtual IDictionary<CultureInfo, string> LanguageValues
{
get
{
return languageValues;
}
}
}
这是实体的自动映射覆盖:
public void Override(AutoMapping<MultilingualPhrase> mapping)
{
mapping
.HasMany(n => n.LanguageValues)
.Access.ReadOnlyPropertyThroughCamelCaseField()
.AsMap<string>("CultureName")
.Element("Phrase")
.Table("MultilingualPhraseValues");
}
此映射(显然)会导致以下错误:
无法将参数值从 CultureInfo 转换为字符串。
我知道 NHibernate 有一个 CultureInfo
的类型自定义类型实现(我使用它来映射属性),但是如何在我的映射覆盖中指定它?
I have dictionary which I'm mapping using Fluent NHibernate. The dictionary has a complex key type CultureInfo
. My database can't store that type so I want to use a string representation of it.
In mappings other than dictionary mappings, I can successfully map CultureInfo
-properties using a user type convention. Now I wonder how to do it for dicationary mappings.
Here's the entity that contains the dictionary:
public class MultilingualPhrase : Entity
{
private IDictionary<CultureInfo, string> languageValues;
public virtual IDictionary<CultureInfo, string> LanguageValues
{
get
{
return languageValues;
}
}
}
And here's the auto mapping override for the entity:
public void Override(AutoMapping<MultilingualPhrase> mapping)
{
mapping
.HasMany(n => n.LanguageValues)
.Access.ReadOnlyPropertyThroughCamelCaseField()
.AsMap<string>("CultureName")
.Element("Phrase")
.Table("MultilingualPhraseValues");
}
This mapping (obviously) causes the following error:
Failed to convert parameter value from a CultureInfo to a String.
I know NHibernate has a type custom type implementation for CultureInfo
(I'm using it for mapping properties) but how do I specify it in my mapping override?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这适用于 NH 3.1 和 FNH 1.2 中的 FNH ClassMap(不确定自动映射):
This works fine with FNH ClassMap (not sure about automapping) in NH 3.1 and FNH 1.2: