流畅的NHibernate映射用于本地化

时间:2011-01-14 09:18:06

标签: nhibernate fluent-nhibernate nhibernate-mapping fluent

我正在尝试从NHibernate映射构建数据库并遇到问题。

我有许多具有本地化字符串值的类:

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; }
}

和语言

public class Language {
    public virtual string Code { get; set }
    public virtual string Name { get; set }
}

我的ShortString和LongString类看起来都一样:

public class ShortString {
    public virtual int Id { get; set; }
    public virtual IDictionary<Language, string> Values { get; set; }
}

我想要实现的是两个表(ShortString和LongString),如下所示:

TABLE ShortString
-----------------
Id (int)
LanguageCode (nvarchar(8))
Value (nvarchar(256)) (or ntext for the LongString Table)

...使用Id AND LanguageCode作为主键,将ForeignKey作为语言表。

在MyClass1和MyClass2表中,我希望将NameId(int)和DescriptionId(int)列分别映射到ShortString和LongString表。

我完全陷入困境。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

也许你可以完全抛弃短篇和长篇

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; }
}

并使用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))
    }
}

我现在无法测试它,所以可能有调整nessesary