List的流畅 NHIbernate 自动映射?

发布于 2024-08-15 08:59:03 字数 575 浏览 5 评论 0原文

Fluent NHibernate 不喜欢这样,会抛出错误:

{“关联引用未映射的类:System.String”}

好吧,我明白为什么这会导致问题 - 但最好的解决方案是什么?

我真的不希望它在单个字段中存储分隔的字符串列表,如果我的列表包含许多字符串,这会变得很难看。

出于显而易见的原因,我也并不真正想要一个表“字符串”。

我想我可以通过将 List 包装在一个类中来解决这个问题,但这感觉有点重量级。我开始认为这是最好的解决方案。

让 Fluent NHibernate 处理这个问题的最佳方法是什么?

我完全希望这些值存储在另一个表中。我想也许我已经能够设置一些自动映射约定来指示 NHibernate '如果您看到一个包含 List<*some Primitive type*>class X,然后继续自动创建映射到该集合的引用表。

将每个集合包装在一个类中感觉有点沉重。如果这是最好的解决方案,那就这样吧。

Fluent NHibernate doesn't like this, throwing an error:

{"Association references unmapped class: System.String"}

OK fine, I can see why this would cause a problem - but what's the best solution?

I don't really want it to store a delimited list of strings in a single field, this would get ugly if my list contains many strings.

I also don't really want a table 'string', for obvious reasons.

I guess I can solve this by wrapping my List<string> inside a class, but this feels a little heavyweight. I'm starting to think its the best solution though.

What's the best way to get Fluent NHibernate to handle this?

I totally expect these values to be stored in another table. I thought perhaps that I may have been able to setup some automapping convention that instructs NHibernate 'If you see a class X that contains List<*some primitive type*>, then go ahead and automatically create a reference table that maps to this collection.

It feels a bit heavy to go and wrap every single collection in a class. If that is the best solution however, then so be it.

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

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

发布评论

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

评论(3

拍不死你 2024-08-22 08:59:03

几周前我遇到了同样的问题,用的是浮点数而不是字符串。

how-do-you-automap-listfloat-或- float-with- Fluent-nhibernate

事实证明,自动映射不适用于原始类型。

编辑 - 这不再是事实 - FNH 团队已经解决了问题

在我的问题的接受答案中有很多示例代码,但关键点是为您的基元列表添加覆盖类型(下面示例中的“RawY”):

public class DlsAppOverlordExportRunData
{
    public virtual int Id { get; set; }
    // Note: List<float> needs overrides in order to be mapped by NHibernate.
    // See class DlsAppOverlordExportRunDataMap.
    public virtual IList<float> RawY { get; set; }
}


// Must be in different namespace from DlsAppOverlordExportRunData!!!
public class DlsAppOverlordExportRunDataMap : IAutoMappingOverride<DlsAppOverlordExportRunData>
{
    public void Override(AutoMapping<DlsAppOverlordExportRunData> mapping)
    {
        // Creates table called "RawY", with primary key
        // "DlsAppOverlordExportRunData_Id", and numeric column "Value"
        mapping.HasMany(x => x.RawY)
               .Element("Value");
    }
}

我希望使用相同的方法来处理字符串的 IList。

I had this exact same issue a few weeks back, with floats instead of strings.

how-do-you-automap-listfloat-or-float-with-fluent-nhibernate

It turns out that Automapping does not work with primitive types.

Edit - This is no longer true - the FNH team has fixed the problem

There's a lot of sample code in the accepted answer to my question, but the key point is to add an override for your lists of primitive types ("RawY" in the example below):

public class DlsAppOverlordExportRunData
{
    public virtual int Id { get; set; }
    // Note: List<float> needs overrides in order to be mapped by NHibernate.
    // See class DlsAppOverlordExportRunDataMap.
    public virtual IList<float> RawY { get; set; }
}


// Must be in different namespace from DlsAppOverlordExportRunData!!!
public class DlsAppOverlordExportRunDataMap : IAutoMappingOverride<DlsAppOverlordExportRunData>
{
    public void Override(AutoMapping<DlsAppOverlordExportRunData> mapping)
    {
        // Creates table called "RawY", with primary key
        // "DlsAppOverlordExportRunData_Id", and numeric column "Value"
        mapping.HasMany(x => x.RawY)
               .Element("Value");
    }
}

I would expect the same approach to work with ILists of strings.

八巷 2024-08-22 08:59:03

自从我发布第一个答案以来,Fluent NHibernate 团队已经解决了这个问题。

现在,您可以自动映射 C# 值类型(字符串、整数、浮点数等)的 IList。

只需确保您拥有最新版本的 FNH。

Since I posted my first answer, the Fluent NHibernate team have fixed this problem.

You can now automap ILists of C# value types (strings, ints, floats, etc).

Just make sure you have a recent version of FNH.

酸甜透明夹心 2024-08-22 08:59:03

这样想……如果没有休眠,你会怎么做?好吧,您可能有一个带有外键和字符串列的表?现在,如何使用 Hibernate 做到这一点?您设置另一个具有多对一和字符串属性的类。然后映射该类的集合。

Think of it this way...how would you do it without hibernate? Well, You'd probably have a table with a foreign-key and string column? Now, how do you do that with Hibernate? You setup another class with a many-to-one and string property. Then you map a collection of that class.

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