用于本地化的流畅 NHibernate 映射

发布于 2024-10-11 16:27:39 字数 1199 浏览 2 评论 0原文

我正在尝试从 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; }
}

像 My ShortString 和 LongString 类这样的语言

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

看起来都一样:

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 作为主键,语言表的外键。

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

我完全被困住了。我怎样才能实现这个目标?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-10-18 16:27:40

也许你可以完全放弃短字符串和长字符串

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

并使用以下映射

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

我现在无法测试它所以可能需要调整

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文