我正在创建一个用户类并想添加一个 ConfirmPassword 字段。由于我的数据库中没有此字段,我该如何处理?我也在使用 AutoMapper,我是否需要做任何事情来处理这个额外的字段,例如告诉映射器忽略这个字段?
我在这里有我的用户类,我刚刚在好友类中添加了 NotMapped 属性。这就是我需要做的所有事情吗?或者是否需要任何额外的编码来处理这种情况?
public partial class User
{
public User()
{
//this.DateCreated = DateTime.Now; //set default value
Roles = new HashSet<Role>();
}
public ICollection<Role> Roles { get; set; } //many to many relationship
public int UserId { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
public string City { get; set; }
//foreign key
public int CountryId { get; set; }
//navigation properties
public virtual Country Country { get; set; }
//foreign key
public int LanguageId { get; set; }
//navigation properties
public virtual Language Language { get; set; }
public string EmailAddress { get; set; }
public long FacebookId { get; set; }
public DateTime DateCreated { get; set; }
}
//buddy class, validation in here because not supported in Fluent API
//test in ie
//MetadataType decorated class
[MetadataType(typeof(UserMetadata))]
public partial class User
{
}
//Metadata type
internal sealed class UserMetadata
{
[Required]
public string FirstName { get; set; }
[Required]
public string Surname { get; set; }
[Required]
[Remote("IsUsernameAvailable", "Validation")]
[DataType(DataType.Text)]
[DisplayName("Username")]
public string Username { get; set; }
[Required]
public int CountryId { get; set; }
public string Password { get; set; }
[NotMapped]
public string ConfirmPassword { get; set; }
public string City { get; set; }
public int LanguageId { get; set; }
public string EmailAddress { get; set; }
public long FacebookId { get; set; }
public DateTime DateCreated { get; set; }
}
}
编辑:这是我相应的 DTO 类:
public class UserDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string City { get; set; }
public string CountryName { get; set; }
public string LanguageName { get; set; }
public string EmailAddress { get; set; }
public long FacebookId { get; set; }
public DateTime DateCreated { get; set; }
}