IEnumerable到EntityCollection使用嵌套映射与自动映射器进行映射失败 [英] IEnumerable to EntityCollection failed mapping with automapper using nested mapping

查看:102
本文介绍了IEnumerable到EntityCollection使用嵌套映射与自动映射器进行映射失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从2.2.0开始,我在使用nasted映射时遇到问题.

Starting with 2.2.0 I'm having a problem with nasted mapping.

我需要映射两个模型:一个EntityObject模型(由EF从DB自动生成)和一个简单的数据模型. EntityObject模型包含另一个EntityObject模型类型的EntityCollection属性,Data Model包含另一个Data Model类型的IEnumerable:这些字段也应该被映射.例如:

I have two models which I need to map: an EntityObject model (autogenerated by EF from DB) and a simple Data Model. EntityObject model contains an EntityCollection property of another EntityObject model type, Data Model contains an IEnumerable of another Data Model type: these fields should also be mapped. As an example:

public class AnotherDataModel
{
    //Some properties
}

public class DataModel
    {
        //Some properties

        private IEnumerable<AnotherDataModel> anotherDataModel;
        public IEnumerable<AnotherDataModel> AnotherDataModel
        {
                get { return anotherDataModel ?? (anotherDataModel = new AnotherDataModel[0]); }
                set { anotherDataModel = value; }
        }
    }

public partial class AnotherModel : EntityObject
{
        //Some properties
}

public partial class Model : EntityObject
    {
        //Some properties

    public EntityCollection<AnotherModel> AnotherModel
        {
                get
                {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<AnotherModel>(//relationship settings);
                }
                set
                {
                if ((value != null))
                    {
                        ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<AnotherModel>(//relationship settings, value);
                    }
                }
            }
        }
    }

我需要将DataModel映射到Model.两种所需的地图都存在:

I need to map DataModel to Model. Both needed maps exist:

Mapper.CreateMap<AnotherDataModel, AnotherModel>();
Mapper.CreateMap<DataModel, Model>();

但是在将DataModel映射到Model时,我在将AnotherDataModel映射到AnotherModel属性时遇到错误:

But while mapping DataModel to Model i'm getting an error on mapping AnotherDataModel to AnotherModel properties:

EntityCollection已被初始化.应该只在对对象图进行反序列化期间调用InitializeRelatedCollection方法来初始化新的EntityCollection.

The EntityCollection has already been initialized. The InitializeRelatedCollection method should only be called to initialize a new EntityCollection during deserialization of an object graph.

在automapper 2.0上工作正常.我为该字段尝试了UseDestinationValue-但结果是相同的.

it works fine on automapper 2.0. I tried UseDestinationValue for this field - but the result is the same.

在其他地方映射IEnumerable时,我也遇到很多集合大小固定"的错误,即使有关此类问题的问题已解决,但我已使用自定义解析器对其进行了修复:

I also got lots of "collection was of a fixed size" errors on mapping IEnumerable in other places, even though an issue about such trouble was closed, but i've fixed it with custom resolver:

public class EnumerableResolver<TCollectionOfInputType, TCollectionOfOutputType> : 
ValueResolver<IEnumerable<TCollectionOfInputType>, IEnumerable<TCollectionOfOutputType>>
{
    public IEnumerable<TCollectionOfOutputType> Resolve(IEnumerable<TCollectionOfInputType> source)
    {
        return this.ResolveCore(source);
    }

    protected override IEnumerable<TCollectionOfOutputType> ResolveCore(IEnumerable<TCollectionOfInputType> source)
    {
        return source == null
            ? null
            : source.Select(Mapper.Map<TCollectionOfInputType, TCollectionOfOutputType>);
    }

在这种情况下不起作用.另外,在不嵌套的情况下将IEnumerable映射到EntityCollection-效果很好:

It doesn't work in this case. Also, mapping IEnumerable to EntityCollection while they are not nested - works fine:

Mapper.CreateMap<IEnumerable<AnotherDataModel>, EntityCollection<AnotherModel>>();

任何帮助将不胜感激.

推荐答案

这是automapper中的一个错误,现已在最新版本中修复.

This was a bug in automapper which is now fixed in latest versions.

在github上发布详细信息,以防万一: https://github.com/AutoMapper/AutoMapper /issues/425

Issue on github with details just in case: https://github.com/AutoMapper/AutoMapper/issues/425

这篇关于IEnumerable到EntityCollection使用嵌套映射与自动映射器进行映射失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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