实体框架变更跟踪和自动映射

时间:2017-02-26 18:12:33

标签: entity-framework-6 automapper-5

实体框架类:

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和DAL模型。 自动映射配置是:

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

存储库类:

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

主要条目:

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

在我的项目中,我有ORM,DAL图层。内部更新方法我想只更新更改的值。当然,如果像ORM.Car那样直接更改:

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

这很有效。实体框架只能更新Name属性和相关的Wheel对象。 但在我的项目中,我有不同的层次。所以我想更改DAL.Car的一些属性,而不是使用Automapper将此对象映射到ORM.Car,并应用像上面的ORM.Car那样的更改。但是在使用Automapper进行映射后,我无法做到这一点,Automapper的原因是在映射后创建了新对象,并且实体框架不能只更新所需的属性,例如ORM.Car直接导致动态代理可能或者我没有我知道。我想要通用的更新,看起来像这样:

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

    Mapper.Map(newCar, entity);

    context.SaveChanges();
} 

其中newCar是从DAL.Car转换而来的Car; 我可以这样做吗?

0 个答案:

没有答案