问题描述
我有很多具有本地化字符串值的类:
public class MyClass1 {
public virtual int Id {get;组; }
public virtual ShortString Name {get;组; }
public virtual LongString描述{get;组; }
}
public class MyClass2 {
public virtual int Id {get;组; }
public virtual ShortString Name {get;组; }
public virtual LongString描述{get;组; }
}
和
public class Language {
public virtual string Code {get; set}
public virtual string Name {get;设置}
}
我的ShortString和LongString类看起来都一样:
public class ShortString {
public virtual int Id {get;组; }
public virtual IDictionary< Language,string>值{get;组; }
$ b我想要实现的是两个表(ShortString和LongString),看起来像这:
TABLE ShortString
-----------------
Id(int)
LanguageCode(nvarchar(8))
Value(nvarchar(256))(或者LongString表的ntext)
pre>
...以Id和LanguageCode作为主键,并将ForeignKey键添加到语言表中。
在MyClass1和MyClass2表中,我希望将NameId(int)和DescriptionId(int)列分别映射到ShortString和LongString表。
我完全被卡住了。如何才能做到这一点?
解决方案也许你可以沟通短和长的字符串
public class MyClass1 {
public virtual int Id {get;组; }
public virtual IDictionary< Language,string>名称{get;组; }
public virtual IDictionary< Language,string>说明{get;组; }
}
public class MyClass2 {
public virtual int Id {get;组; }
public virtual IDictionary< Language,string>名称{get;组; }
public virtual IDictionary< Language,string>说明{get;组; }
}
并使用folling映射
public class MyClass1Map:ClassMap< MyClass1> ...
{
public MyClass1Map()
{
[...]
HasMany(mc => mc.Name)
.Table(ShortString )
.KeyColumn(id)
.AsEntityMap(language_id)
.Element(value)
HasMany(mc => mc.Description)
.Table(LongString)
.KeyColumn(id)
.AsEntityMap(language_id)
.Element(value,e => e.Length (1000))
}
}
现在我无法测试可能会调整nebsary
I am trying to build a Database from NHibernate mappings and have run into a problem.
I have many classes with localized string values:
public class MyClass1 {
public virtual int Id { get; set; }
public virtual ShortString Name { get; set; }
public virtual LongString Description { get; set; }
}
public class MyClass2 {
public virtual int Id { get; set; }
public virtual ShortString Name { get; set; }
public virtual LongString Description { get; set; }
}
and Languages like
public class Language {
public virtual string Code { get; set }
public virtual string Name { get; set }
}
My ShortString and LongString classes both look the same:
public class ShortString {
public virtual int Id { get; set; }
public virtual IDictionary<Language, string> Values { get; set; }
}
What I want to achieve are two tables (ShortString and LongString) looking like this:
TABLE ShortString
-----------------
Id (int)
LanguageCode (nvarchar(8))
Value (nvarchar(256)) (or ntext for the LongString Table)
...with Id AND LanguageCode as primary keys and a ForeignKey to the Language Table.
And in the MyClass1 and MyClass2 tables, I want to have NameId (int) and DescriptionId (int) columns mapped to ShortString and LongString tables respectively.
I am totally stuck. How can I achieve this?
解决方案 Maybe you could ditch short and long string altogether
public class MyClass1 {
public virtual int Id { get; set; }
public virtual IDictionary<Language, string> Name { get; set; }
public virtual IDictionary<Language, string> Description { get; set; }
}
public class MyClass2 {
public virtual int Id { get; set; }
public virtual IDictionary<Language, string> Name { get; set; }
public virtual IDictionary<Language, string> Description { get; set; }
}
and use the folling Mapping
public class MyClass1Map : ClassMap<MyClass1>
{
public MyClass1Map()
{
[...]
HasMany(mc => mc.Name)
.Table("ShortString")
.KeyColumn("id")
.AsEntityMap("language_id")
.Element("value")
HasMany(mc => mc.Description)
.Table("LongString")
.KeyColumn("id")
.AsEntityMap("language_id")
.Element("value", e => e.Length(1000))
}
}
I cant test it right now so there might be tweaking nessesary
这篇关于流利的NHibernate映射本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!