[英]How to use Automapper with Object in Object
I want to Map two types of Object but I didn't find the way to do it.我想 Map 两种类型的 Object 但我没有找到方法。
User class用户 class
public partial class TUser
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("login")]
[StringLength(50)]
public string Login { get; set; }
[Column("password")]
[StringLength(50)]
public string Password { get; set; }
[Column("role")]
[StringLength(50)]
public string Role { get; set; }
[Column("isDeleted")]
public bool? IsDeleted { get; set; }
[Column("avatarUrl")]
[StringLength(50)]
public string AvatarUrl { get; set; }
[Column("iso")]
[StringLength(2)]
public string Iso { get; set; }
[Column("lastLogonDate", TypeName = "datetime")]
public DateTime? LastLogonDate { get; set; }
[Column("createdDate", TypeName = "datetime")]
public DateTime? CreatedDate { get; set; }
[Column("lastUpdatedDate", TypeName = "datetime")]
public DateTime? LastUpdatedDate { get; set; }
public byte[] PasswordHash { get; set; }
public byte[] PasswordSalt { get; set; }
[InverseProperty("IdNavigation")]
public virtual TWorker TWorker { get; set; }
}
UserForLogin Class UserForLogin Class
public class UserForLogin
{
public int Id { get; set; }
public string Login { get; set; }
public string Role { get; set; }
public string AvatarUrl { get; set; }
public string Iso { get; set; }
public TWorker TWorker { get; set; }
}
TWorker class TWorker class
public partial class TWorker
{
public TWorker()
{
TWorkerToWorkType = new HashSet<TWorkerToWorkType>();
TrEventToWorker = new HashSet<TrEventToWorker>();
TrWorkerToWorkerCategory = new HashSet<TrWorkerToWorkerCategory>();
}
[Key]
[Column("id")]
public int Id { get; set; }
[Column("lastname")]
[StringLength(50)]
public string Lastname { get; set; }
[Column("firstname")]
[StringLength(50)]
public string Firstname { get; set; }
[Column("email")]
[StringLength(50)]
public string Email { get; set; }
[Column("phone")]
[StringLength(50)]
public string Phone { get; set; }
[Column("address")]
[StringLength(50)]
public string Address { get; set; }
[Column("postcode")]
[StringLength(50)]
public string Postcode { get; set; }
[Column("locality")]
[StringLength(50)]
public string Locality { get; set; }
[Column("workerCategoryKey")]
public int? WorkerCategoryKey { get; set; }
[Column("sexe")]
[StringLength(50)]
public string Sexe { get; set; }
[ForeignKey(nameof(Id))]
[InverseProperty(nameof(TUser.TWorker))]
public virtual TUser IdNavigation { get; set; }
[InverseProperty("WorkerKeyNavigation")]
public virtual ICollection<TWorkerToWorkType> TWorkerToWorkType { get; set; }
[InverseProperty("WorkerKeyNavigation")]
public virtual ICollection<TrEventToWorker> TrEventToWorker { get; set; }
[InverseProperty("WorkerKeyNavigation")]
public virtual ICollection<TrWorkerToWorkerCategory> TrWorkerToWorkerCategory { get; set; }
}
AutoMapperProfiles class AutoMapperProfiles class
public AutoMapperProfiles()
{
CreateMap<TUser, UserForLogin>()
.ForMember(
dest => dest.TWorker,
opt => opt.MapFrom(src => src.TWorker)
);
}
But TWorker is always null and I can't find what am I doing wrong?但是 TWorker 总是 null 我找不到我做错了什么? If I use TUser only to return my object without Automapper code, TWorker contains the values I want.
如果我只使用 TUser 来返回没有 Automapper 代码的 object,则 TWorker 包含我想要的值。
Thanks for your help:-)谢谢你的帮助:-)
You just need to implement the map for the subObject and autoMapper will handle it for you.您只需为子对象实现 map,autoMapper 将为您处理它。 To be precise, if you map a property to another property which has a different type, autoMapper will try to find a corresponding map.
准确地说,如果您将一个属性 map 转换为另一个具有不同类型的属性,autoMapper 将尝试找到相应的 map。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.