1
Automapper를 Entity Framework Code First 4.1과 함께 사용하려고합니다. 나는 100 % 시간을 정확하게 저장하기 위해 저장/업데이트를 얻을 수 없다.Automapper 및 Entity Framework 코드가 올바르게 작동하지 않습니다.
첫 번째 문제는 개체에 UseDestinationValue()
을 사용하지 않으면 내 DTO가 해당 속성을 엔터티에 매핑하지 않는다는 것입니다. 그런 다음 UseDestinationValue()
을 사용하지 않으면 동적으로 생성 된 프록시 개체가 일부 속성을 잃고 Address 속성은 모든 업데이트에서 새 엔터티를 가져옵니다.
Mapper.CreateMap<GymModel, Gym>(); <-- loses Entity Framework proxy properties for update, works fine on insert
또는
Mapper.CreateMap<GymModel, Gym>()
.ForAllMembers(q => q.UseDestinationValue()); <-- Doesn't map DTO to Entity
및/또는이
var gym = Mapper.CreateMap<GymModel, Gym>();
gym.ForAllMembers(q => q.UseDestinationValue());
gym.ForMember(q => q.DateCreated, o => o.MapFrom(s => s.DateCreated));
서비스
public void Save(GymModel model)
{
var item = new Gym()
{
Address = new Address()
};
if (model.Id > 0)
{
Expression<Func<Gym, bool>> where = q => q.Id == model.Id;
Expression<Func<Gym, object>> address = q => q.Address;
item = _gymsRepository.Get(new [] { address }, where);
}
model.Address.Id = item.AddressId;
Mapper.Map(model, item);
AddressModel 중 하나를 작동하지 않습니다
public class AddressModel : IAddressModel
{
public int Id { get; set; }
public string Location { get; set; }
public string StreetAddress { get; set; }
public string ExtendedAddress { get; set; }
public string City { get; set; }
public string StateRegion { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
}
주소
[Table("Address", Schema = "GrassrootsHoops")]
public class Address
{
[Key]
public int Id { get; set; }
public string Location { get; set; }
public string StreetAddress { get; set; }
public string ExtendedAddress { get; set; }
public string City { get; set; }
public string StateRegion { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
}
비슷한 문제가 있습니다. 이 접근법보다 나은 해결책을 찾았습니까? – Yuck