asp.net-mvc – 如何使用启用了延迟加载的Automapper和EF4 DbContext从ViewModel更新现有实体

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 如何使用启用了延迟加载的Automapper和EF4 DbContext从ViewModel更新现有实体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Automapper在Entity和viewmodel对象之间进行映射(在两个方向上).该模型使用EF4 DbContext POCO并需要启用LazyLoading(因此代理生成).

我在尝试从viewmodel更新现有实体时遇到了问题.当我调用Mapper.Map(vm,entity)时,Automapper会抛出异常.我的问题是:你如何使用Automapper使用EF代理对象?

代码看起来(简化)像这样:

public class MyEntity
{
    public int Id {get;set;}
    public int Name {get;set;}
}

public class viewmodel
{
    public int Id {get;set;}
    public int Name {get;set;}
}

Mapper.CreateMap<MyEntity,viewmodel>();
Mapper.CreateMap<viewmodel,MyEntity>();

public ActionResult Edit(viewmodel vm)
{
    MyEntity entity = db.MyEntities.Find(vm.Id);
    Mapper.Map(vm,entity);
    db.Entry(entity).State = EntityState.Modified;
    db.SaveChanges();
}

当我调用Mapper.Map(vm,entity)来更新现有的实体对象时,我得到了异常:

'Mapper.Map(viewmodel,resultSet)' threw an exception of type 'AutoMapper.AutoMapperMappingException'
base {System.Exception}: {"Missing type map configuration or unsupported mapping.\n\nMapping types:\r\nResultSetView -> ResultSet_692D75838D4DC59B922F3E88CF1B10516CBF6CD8A32C4BE2F3FCC28CE83F0BD2\r\nSciensus.Applications.ClinicalStudies.Web.Areas.Patient.Models.ResultSetView -> System.Data.Entity.DynamicProxies.ResultSet_692D75838D4DC59B922F3E88CF1B10516CBF6CD8A32C4BE2F3FCC28CE83F0BD2\n\nDestination path:\nResultSet_692D75838D4DC59B922F3E88CF1B10516CBF6CD8A32C4BE2F3FCC28CE83F0BD2\n\nSource value:\nSciensus.Applications.ClinicalStudies.Web.Areas.Patient.Models.ResultSetView"}
Context: {Trying to map ResultSetView to ResultSet_692D75838D4DC59B922F3E88CF1B10516CBF6CD8A32C4BE2F3FCC28CE83F0BD2.}
Message: "Missing type map configuration or unsupported mapping.\n\nMapping types:\r\nResultSetView -> ResultSet_692D75838D4DC59B922F3E88CF1B10516CBF6CD8A32C4BE2F3FCC28CE83F0BD2\r\nSciensus.Applications.ClinicalStudies.Web.Areas.Patient.Models.ResultSetView -> System.Data.Entity.DynamicProxies.ResultSet_692D75838D4DC59B922F3E88CF1B10516CBF6CD8A32C4BE2F3FCC28CE83F0BD2\n\nDestination path:\nResultSet_692D75838D4DC59B922F3E88CF1B10516CBF6CD8A32C4BE2F3FCC28CE83F0BD2\n\nSource value:\nSciensus.Applications.ClinicalStudies.Web.Areas.Patient.Models.ResultSetView"
StackTrace: ""

解决方法

我查看了AutoMapper源代码
public TDestination Map<TSource,TDestination>(TSource source,TDestination destination)
    {
        return Map(source,destination,opts => { });
    }

    public TDestination Map<TSource,TDestination destination,Action<IMappingOperationOptions> opts)
    {
        Type modelType = typeof(TSource);
        Type destinationType = (Equals(destination,default(TDestination)) ? typeof(TDestination) : destination.GetType());

        return (TDestination)Map(source,modelType,destinationType,opts);
    }

这个地方出现了问题:

Type destinationType = (Equals(destination,default(TDestination)) ? typeof(TDestination) : destination.GetType());

所以没有问题的变化:

Mapper.Map(vm,entity,typeof(viewmodel),typeof(MyEntity));

猜你在找的asp.Net相关文章