Fluent NHibernate mappings for localization

我与影子孤独终老i 提交于 2019-12-12 10:10:01

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/4689547/fluent-nhibernate-mappings-for-localization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!