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

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

问题描述


$ b


{Association references unmapped class:System.String}


好的,我明白为什么会造成问题 - 但最好的解决方案是什么?



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

我也不是真的想要一个表字符串,原因很明显。



我想我可以通过在类中包装 List< string> 来解决这个问题,重量级。我开始认为它是最好的解决方案。



获得流利的NHibernate来处理这个问题的最好方法是什么?



我完全期望这些值被存储在另一个表中。我想也许我可能已经能够设置一些自动映射约定来指导NHibernate如果你看到一个 class X ,它包含 List< *一些原语键入*> ,然后继续并自动创建映射到此集合的参考表。

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

解决方案


在几周前,

如何做-automap-listfloat-or-float-with-fluent-nhibernate

事实证明,Automapping不能用于原始类型。



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

在我的问题的接受答案的示例代码,但关键是要添加一个基本类型的列表(下面的例子中的RawY)的重写:

  public class DlsAppOverlordExportRunData 
{
public virtual int Id {get;组; }
//注意:列表< float>需要覆盖,以便由NHibernate映射。
//见类DlsAppOverlordExportRunDataMap。
公共虚拟IList< float> RawY {get;组; }
}


//必须位于DlsAppOverlordExportRunData的不同命名空间中!
public class DlsAppOverlordExportRunDataMap:IAutoMappingOverride< DlsAppOverlordExportRunData>
{
public void Override(AutoMapping< DlsAppOverlordExportRunData>映射)
{
//创建名为RawY的表,主键
//DlsAppOverlordExportRunData_Id和数值列Value
mapping.HasMany(x => x.RawY)
.Element(Value);


$ / code $ / pre

我希望同样的方法可以和ILists字符串。


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.

解决方案

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.

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

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