自动映射的IDictionary<字符串,MyClass的>用流利的NHibernate [英] Auto mapping a IDictionary<string, myclass> with fluent nhibernate

查看:183
本文介绍了自动映射的IDictionary<字符串,MyClass的>用流利的NHibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的类,看起来像这样:

I have a simple class that looks like this:

public class Item {
 // some properties
public virtual IDictionary<string, Detail> Details { get; private set; }
}



然后我有地图,看起来像这样:

and then I have a map that looks like this:

map.HasMany(x => x.Details).AsMap<string>("Name").AsIndexedCollection<string>("Name", c => c.GetIndexMapping()).Cascade.All().KeyColumn("Item_Id"))

这个地图我碰到下面的错误,我不知道该如何解决呢?

with this map I get the following error and I don't know how to solve it?

该类型或方法有2个泛型参数(S)但1一般的参数(S)的规定。必须为每个泛型参数的通用说法。

The type or method has 2 generic parameter(s), but 1 generic argument(s) were provided. A generic argument must be provided for each generic parameter.

推荐答案

我发现了一个办法解决这个。基本上我阻止automapper从尝试映射一个IDictionary。这迫使我不得不在一个覆盖手动映射,但至少它的工作原理。

I found a workaround for this. Basically I'm preventing the automapper from attempting to map an IDictionary. It forces me to have to map it manually in an override but at least it works.

我使用的是从DefaultAutomappingConfiguration派生的AutomappingConfiguration。

I'm using an AutomappingConfiguration derived from DefaultAutomappingConfiguration.

public override bool ShouldMap(Member member)
{
    if ( member.PropertyType.IsGenericType )
    {
        if (member.PropertyType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.IDictionary<,>))
                    return false;
    }

    return base.ShouldMap(member);
}

和这里的一对夫妇示例类和相关的映射,我使用要做到这一点:

And here's a couple of sample classes and the associated mapping that I'm using to make this happen:

public class ComponentA
{
    public virtual string Name { get; set; }
}

public class EntityF : Entity
{
    private IDictionary<string, ComponentA> _components = new Dictionary<string, ComponentA>();
    public IDictionary<string, ComponentA> Components
    {
        get { return _components; }
        set { _components = value; }
    }
}

public class EntityFMap : IAutoMappingOverride<EntityF>
{
    public void Override(AutoMapping<EntityF> mapping)
    {
        mapping.HasMany<ComponentA>(x => x.Components)
            .AsMap<string>("IndexKey")
            .KeyColumn("EntityF_Id")
            .Table("EntityF_Components")
            .Component(x =>
                           {
                               x.Map(c => c.Name);
                           })
            .Cascade.AllDeleteOrphan();
    }
}



我刚刚花了几个小时,使这项工作,所以我希望这样可以节省别人的拔头发的夜晚。

I've just spent several hours to make this work, so I hope this saves someone else an evening of hair-pulling.

这篇关于自动映射的IDictionary&LT;字符串,MyClass的&GT;用流利的NHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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