2

Entity Framework classes:

namespace ORM
{
    public class Car
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int Price { get; set; }

        public virtual List<Wheel> Wheels { get; set; }
    }
    public class Wheel
    {
        public int Id { get; set; }

        public double Size { get; set; }

        public virtual Car Car { get; set; }
    }
}
namespace DAL.Entities
{
    public class Car
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int Price { get; set; }

        public List<Wheel> Wheels { get; set; }
    }
    public class Wheel
    {
        public int Id { get; set; }

        public double Size { get; set; }

        public Car Car { get; set; }
    }
}

ORM and DAL models. Automapper configuration is:

public class DalProfile : Profile
    {
        public DalProfile()
        {
            CreateMap<Wheel, Entities.Wheel>()
                .ForMember(m => m.Car, opt => opt.Ignore());

            CreateMap<Entities.Wheel, Wheel>()
                .ForMember(m => m.Car, opt => opt.Ignore());

            CreateMap<Car, Entities.Car>()
                .AfterMap((src, dest) =>
                {
                    foreach (var wheel in dest.Wheels)
                    {
                        wheel.Car = dest;
                    }
                });

            CreateMap<Entities.Car, Car>()
                .AfterMap((src, dest) =>
                {
                    foreach (var wheel in dest.Wheels)
                    {
                        wheel.Car = dest;
                    }
                });

            CreateMap<Wheel, Wheel>()
                .ForMember(m => m.Car, opt => opt.Ignore());

            CreateMap<Car, Car>()
                .AfterMap((src, dest) =>
                {
                    foreach (var wheel in dest.Wheels)
                    {
                        wheel.Car = dest;
                    }
                });
        }
    }

Repository class:

public class CarRepository
    {
        private readonly DbContext context;

        public CarRepository(DbContext context)
        {
            this.context = context;
        }

        public void Update<T>(int id, T newCar) where T : class
        {
            var entity = context.Set<T>().Find(id);

            Mapper.Map(newCar, entity);

            context.SaveChanges();
        }
}

Main entry:

static void Main(string[] args)
        {
            Mapper.Initialize(cfg => cfg.AddProfile(new DalProfile()));
            DataContext context = new DataContext();
            CarRepository carRepository = new CarRepository(context);

            Car car = carRepository.Get(120);
            DAL.Entities.Car dalCar = Mapper.Map<Car, DAL.Entities.Car(car);

            dalCar.Name = "ew";
            dalCar.Wheels[0].Size = 1994;
            Car ormCar = Mapper.Map<DAL.Entities.Car, Car>(dalCar);

            carRepository.Update(ormCar.Id, ormCar);
        }

In my project i have ORM, DAL layers. Inside Update method i want to update only changed values. Of course if do changes with ORM.Car directly like this:

public void Update<T>(ORM.Car car) where T : class
        {
            var entity = context.Set<Car>().Find(car.id);
            entity.Name = "New name";
            entity.Wheels[0].Size = 1111;
            context.SaveChanges();
        }

This works great. Entity framework can update only Name property and related Wheel object. But in my project i have different layers. So i want to change some properties of DAL.Car than map this object to ORM.Car with Automapper and apply changes like i did with ORM.Car above. But after mapping with Automapper i can't do this cause of Automapper creates new objects after mapping and Entity Framework can't update only needed properties like with ORM.Car directly cause of Dynamic Proxies maybe or i don't know. I want generic Update which looks something like this:

public void Update<T>(int id, T newCar) where T : class
{
    var entity = context.Set<T>().Find(id);

    Mapper.Map(newCar, entity);

    context.SaveChanges();
} 

Where newCar is a Car which is converted from DAL.Car; Can i do this?

ZingerCar
  • 33
  • 1
  • 7
  • AutoMapper can also map to existing objects. That said, it's generally not recommended to use AM to map view models/DTOs etc. back to entities, because this usually entails business rules to be obeyed, or the models are not complete, so you may get tempted to make the AM mapping *very* complex. – Gert Arnold Feb 26 '17 at 22:11
  • There is got to be a setting/property that can be set on automapper that allows entity framework to do savechanges or updates without creating a new object. – Mohammad S. May 13 '17 at 06:18

0 Answers0