How to update an existing Entity from ViewModel using Automapper and EF4 DbContext with Lazy Loading enabled

六月ゝ 毕业季﹏ 提交于 2019-12-04 04:56:52

I looked at the AutoMapper source Code:

    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);
    }

Problems occurred in this place:

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

So the change that do not have a problem:

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

As you suspect, I believe you are getting this exception, because AutoMapper doesn't have a map for the proxy class created by Lazy Loading (ResultSet_692D75838D4DC59B922F3E88CF1B10516CBF6CD8A32C4BE2F3FCC28CE83F0BD2) which derives from your ResultSet entity.

What you might try is the following:

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();
}

So what I ended up doing was rolling my own IMappable interface and a simple generic mapping utility to support two-way mapping. Depending on the complexity of the mapping, the code required can end up being less than with Automapper. Code below:

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);
    }
}

My ViewModels implement IMappable for as many Entity classes as necessary, implementing a Map method to handle data transfer in each direction:

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!