我在嘗試使用複雜類型時遇到問題AutoMapper。複雜類型的AutoMapper
我有兩個對象,一個域和ViewModel utlize在我的視圖;
視圖模型
public class RegisterCoupleModel
{
[Required(ErrorMessage = "campo obrigatório")]
[Display(Name = "Primeiro nome", Order = 0, GroupName = "Noivo")]
[StringLength(60)]
public string FistNameGroom
{
get;
set;
}
[Required(ErrorMessage = "campo obrigatório")]
[Display(Name = "Último nome", Order = 1, GroupName = "Noivo")]
[StringLength(60)]
public string LastNameGroom
{
get;
set;
}
[Required(ErrorMessage = "campo obrigatório")]
[RegularExpression(@"^[\w-\.][email protected]([\w-]+\.)+[\w-]{2,4}$", ErrorMessage = "Formato de email inválido.")]
[DataType(DataType.EmailAddress)]
[Remote("IsEmailAvailable", "Validation", "", ErrorMessage = "este email já está sendo usado por outro usuário.")]
[Display(Name = "Email", Order = 2, GroupName = "Noivo")]
[StringLength(180)]
public string EmailGroom
{
get;
set;
}
[Required(ErrorMessage = "campo obrigatório")]
[Display(Name = "Primeiro nome", Order = 0, GroupName = "Noiva")]
[StringLength(60)]
public string FistNameBride
{
get;
set;
}
[Required(ErrorMessage = "campo obrigatório")]
[Display(Name = "Último nome", Order = 1, GroupName = "Noiva")]
[StringLength(60)]
public string LastNameBride
{
get;
set;
}
[Required(ErrorMessage = "campo obrigatório")]
[RegularExpression(@"^[\w-\.][email protected]([\w-]+\.)+[\w-]{2,4}$", ErrorMessage = "Formato de email inválido.")]
[DataType(DataType.EmailAddress)]
[Remote("IsEmailAvailable", "Validation", "", ErrorMessage = "este email já está sendo usado por outro usuário.")]
[Display(Name = "Email", Order = 2, GroupName = "Noiva")]
[StringLength(180)]
public string EmailBride
{
get;
set;
}
[Required(ErrorMessage = "campo obrigatório")]
[Display(Name = "Url")]
[RegularExpression(@"^[a-zA-Z0-9_\-\.\+]{5,22}$", ErrorMessage = "Chave inválida")]
[StringLength(22, MinimumLength = 5, ErrorMessage = "A chave deve ter entre {0} e {1} caracteres.")]
[Remote("IsKeywordAvaliable", "Validation")]
public string UrlKeyword
{
get;
set;
}
[Display(Name = "Voce é humano?")]
[UIHint("ReCaptcha")]
public string ReCaptcha
{
get;
set;
}
}
域對象
public class Couple
{
[Key]
public Guid Id { get; set; }
public string UrlKeyword { get; set; }
public virtual Partner Groom { get; set; }
public virtual Partner Bride { get; set; }
public DateTime Marriage { get; set; }
public DateTime Dating { get; set; }
public DateTime Engagement { get; set; }
public virtual ICollection<User> Users { get; set; }
}
如果你看看你會看到我的對象Couple
域有屬性有Bride
和Groom
這實際上是同一類型。
如何映射我的域對象Couple
與RegisterCoupleModel
?
這裏我做到了儘可能:
在設置automapper試着這樣做:
CreateMap<RegisterCoupleModel, Couple>()
.ForMember(dest => dest.Bride.User.FirstName, opt => opt.MapFrom(source => source.FistNameBride))
.ForMember(dest => dest.Bride.User.LastName, opt => opt.MapFrom(source => source.LastNameBride))
.ForMember(dest => dest.Bride.User.Email, opt => opt.MapFrom(source => source.EmailBride))
.ForMember(dest => dest.Groom.User.FirstName, opt => opt.MapFrom(source => source.FistNameGroom))
.ForMember(dest => dest.Groom.User.LastName, opt => opt.MapFrom(source => source.LastNameGroom))
.ForMember(dest => dest.Groom.User.Email, opt => opt.MapFrom(source => source.EmailGroom));
但顯示以下錯誤:
表達「DEST => dest.Bride.User.FirstName'必須解析爲 頂級成員。參數名稱:lambda表達式
我知道這需要嘗試使用映射屬性與嵌套類型的行爲。
但我怎麼能映射到RegisterCoupleModel
和Couple
兩個屬性Bride
和Groom
是同一類型?
我發現question here on StackOverflow看起來像這樣,但它幫助了我。
但是屬性如何? 如果封裝這些字段,新娘和新郎的屬性設置是相同的。 因此,例如,我無法更改顯示文本或GrupName顯示屬性。 – ridermansb
@Riderman - 使用一些OO技巧,例如製作PersonDetails抽象,然後爲BrideDetails和GroomDetails創建派生類。在基礎中設置公共屬性,然後覆蓋不同的屬性。 – RPM1984
我使用助手:'@ Html.EditorForModel()'生成表單,但它不生成嵌套類型。 如何封裝這三個屬性,並使幫助器繼續在窗體中顯示它們? – ridermansb