List<string> 的流畅 NHIbernate 自动映射? [英] Fluent NHIbernate automapping of List&lt;string&gt;?

查看:14
本文介绍了List<string> 的流畅 NHIbernate 自动映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

{"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.

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

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.

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

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

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

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.

推荐答案

几周前我遇到了完全相同的问题,使用浮点数而不是字符串.

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

事实证明 Automapping 不适用于原始类型.

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

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

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

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

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

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

这篇关于List<string> 的流畅 NHIbernate 自动映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆