У меня есть сопоставление для класса модели домена Entity Framework и его класса DTO.
Модель:
public class UserAccount : BaseEntity
{
/// <summary>
/// Default constructor.
/// </summary>
public UserAccount() => Users = new HashSet<User>();
#region Public Properties
/// <summary>
/// The email address of this user account.
/// </summary>
[Required]
[MaxLength(255)]
public string Email { get; set; }
/// <summary>
/// The password of this user account.
/// </summary>
[Required]
[MaxLength(500)]
public string Password { get; set; }
/// <summary>
/// The verified status of this user account.
/// </summary>
public bool Verified { get; set; }
/// <summary>
/// The associated list of <see cref="User"/> for this user account.
/// </summary>
public virtual ICollection<User> Users { get; set; }
#endregion
#region Helpers
public override string ToString()
{
string str = base.ToString();
str +=
$"Email: {Email}{Environment.NewLine}" +
$"Password: {Password}{Environment.NewLine}" +
$"Verified: {Verified}";
return str;
}
#endregion
}
DTO:
public class UserAccountDto
{
/// <summary>
/// The email address of this user account.
/// </summary>
[Required]
[MaxLength(255)]
public string Email { get; set; }
/// <summary>
/// The password of this user account.
/// </summary>
[Required]
[MaxLength(500)]
public string Password { get; set; }
}
Я сопоставил и зарегистрировал их в Global.asax, вот код сопоставления:
// Domain.
CreateMap<UserAccount, UserAccountDto>();
// DTO.
CreateMap<UserAccountDto, UserAccount>()
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.EntityCreated, opt => opt.Ignore())
.ForMember(dest => dest.EntityActive, opt => opt.Ignore())
.ForMember(dest => dest.EntityVersion, opt => opt.Ignore())
.ForMember(dest => dest.Verified, opt => opt.Ignore())
.ForMember(dest => dest.Users, opt => opt.Ignore());
Я пытаюсь сопоставить DTO с доменом так,Я могу сохранить домен в своей базе данных, используя следующий код:
UserAccount userAccount = Mapper.Map<UserAccount>(userAccountDto);
Однако я получаю эту ошибку:
AutoMapper created this type map for you, but your types cannot be mapped using the current configuration.
UserAccountDto -> UserAccount (Destination member list)
OysterCard.Models.Dto.UserAccount.UserAccountDto -> OysterCard.Models.Security.UserAccount (Destination member list)
Unmapped properties:
Verified
Users
Id
EntityCreated
EntityActive
EntityVersion
Что я здесь не так делаю?Я сопоставил вышеупомянутые свойства, поэтому я не уверен, где это идет не так.Я совершенно новичок в AutoMapper, поэтому я могу где-то ошибаться, но я не уверен, где именно.
Если кто-то может помочь мне решить мою проблему, я был бы очень благодарен.
Спасибо.