7

我正在使用 Automapper 在 Entity 和 ViewModel 对象之间进行映射(双向)。该模型使用 EF4 DbContext POCO,需要启用 LazyLoading(因此需要代理生成)。

我在尝试从视图模型更新现有实体时遇到了问题。当我调用 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: ""
4

3 回答 3

6

我查看了 AutoMapper 源代码:

    public TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
    {
        return Map(source, destination, opts => { });
    }

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

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

这个地方出现了问题:

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

所以没有问题的变化:

  Mapper.Map(vm, entity,typeof(ViewModel),typeof(MyEntity));
于 2013-01-11T03:41:34.337 回答
3

正如您所怀疑的那样,我相信您遇到了这个异常,因为 AutoMapper 没有为从您的实体ResultSet_692D75838D4DC59B922F3E88CF1B10516CBF6CD8A32C4BE2F3FCC28CE83F0BD2派生的延迟加载 ()创建的代理类的映射。ResultSet

您可能会尝试以下方法:

public ActionResult Edit(ViewModel vm)
{
    // This returns the entity proxy
    MyEntity oldEntity = db.MyEntities.Find(vm.Id);
    // i.e. Create a 'plain' Entity, not a proxy.
    MyEntity newEntity = Mapper.Map<ViewModel, MyEntity>(vm);

    db.Entry(oldEntity).CurrentValues.SetValues(newEntity);
    // I don't think you need this now.
    // db.Entry(entity).State = EntityState.Modified;
    db.SaveChanges();
}
于 2012-11-16T12:57:35.133 回答
1

所以我最终做的是滚动我自己的 IMappable 接口和一个简单的通用映射实用程序来支持双向映射。根据映射的复杂性,所需的代码最终可能少于 Automapper。下面的代码:

public interface IMappable<TEntity, TViewModel>
{
    void Map(TEntity source, TViewModel target);
    void Map(TViewModel source, TEntity target);
}

public class ModelMapper<TEntity, TViewModel> where TEntity : new() where TViewModel : IMappable<TEntity, TViewModel>, new()
{
    public static TViewModel Map(TEntity source)
    {
        TViewModel target = new TViewModel();
        Map(source, target);
        return target;
    }

    public static TEntity Map(TViewModel source)
    {
        TEntity target = new TEntity();
        Map(source, target);
        return target;
    }

    public static void Map(TEntity source, TViewModel target)
    {
        new TViewModel().Map(source, target);
    }

    public static void Map(TViewModel source, TEntity target)
    {
        new TViewModel().Map(source, target);
    }
}

我的 ViewModel 为尽可能多的 Entity 类实现了 IMappable,实现了一个 Map 方法来处理每个方向的数据传输:

public class AddressView : IMappable<Address, AddressView>
{
    public long AddressId { get; set; }
    ...

    #region "IMappable members"
    public void Map(Address source, AddressView target)
    {
        target.AddressId = source.AddressId;
        target.City = source.City;
        target.FullAddress = source.FullAddress;
        if (source.CountryId.HasValue)
        {
            target.Country = source.Country.Name;
            target.CountryId = source.CountryId;
        }
        target.District = source.District;
        target.Postcode = source.Postcode;
        target.StreetName = source.StreetName;
    }

    public void Map(AddressView source, Address target)
    {
        target.AddressId = source.AddressId;
        target.City = source.City;
        target.CountryId = source.CountryId;
        target.District = source.District;
        target.Postcode = source.Postcode;
        target.StreetName = source.StreetName;
    }
    #endregion
}
于 2012-12-07T11:18:39.157 回答