[英]Automapper and Base classes
首先讓我說我是自動映射器的新手。 我有一個基類,其類繼承如下:
public abstract class Base
{
[Required]
public Guid CreatedBy { get; set; }
[Required]
public DateTime DateCreated { get; set; }
[Required]
public DateTimeOffset DateCreatedOffset { get; set; }
public Guid? UpdatedBy { get; set; }
public DateTime? DateUpdated { get; set; }
public DateTimeOffset? DateUpdatedOffset { get; set; }
[Required]
public bool Deleted { get; set; }
}
public class Customer : Base
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid CustomerId { get; set; }
[Required]
public Guid CompanyId { get; set; }
public virtual Company Company { get; set; }
[Required]
public TypeCustomer CustomerType { get; set; }
[Display(Name = "Customer Name")]
public string CustomerName { get; set; }
[StringLength(20)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[StringLength(20)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[DataType(DataType.EmailAddress)]
public string PrimaryEmail { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
}
要映射的類如下:
public class CustomerObject
{
public Guid CustomerId { get; set; }
public Guid CompanyId { get; set; }
public Guid CreatedBy { get; set; }
public DateTime DateCreated { get; set; }
public DateTimeOffset DateCreatedOffset { get; set; }
public Guid UpdatedBy { get; set; }
public DateTime? DateUpdated { get; set; }
public DateTimeOffset? DateUpdatedOffset { get; set; }
public TypeCustomer CustomerType { get; set; }
public string CustomerName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
[DataType(DataType.EmailAddress)]
public string PrimaryEmail { get; set; }
public virtual ICollection<AddressObject> Addresses { get; set; }
public virtual ICollection<PhoneNumberObject> PhoneNumbers { get; set; }
}
映射類的代碼:
Customer customer = GetCustomerById(customerObject.CustomerId);
Mapper.CreateMap<CustomerObject, Customer>();
customer = Mapper.Map<CustomerObject, Customer>(customerObject);
customer.UpdatedBy = userId;
customer.DateUpdated = DateTime.UtcNow;
customer.DateUpdatedOffset = DateTimeOffset.UtcNow;
嘗試將客戶類的實例映射到客戶對象時,我丟失了基類的所有屬性。 有人碰到這個嗎?
首先,您使用的是哪個版本的AutpMapper? 我假設您正在使用nuget軟件包的最新版本4.0.4
如果是,那么此功能已經存在於automapper中,這是一個小提琴,它表明
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.