模型和视图模型之间的 AutoMapper 错误:缺少类型映射配置或不支持的映射
Error with AutoMapper between model and viewmodel: Missing type map configuration or unsupported mapping
好吧,我正在尝试映射我的用户配置文件模型 to/from 用户配置文件视图模型,但它不会工作,因为它们不共享所有数据字段:
型号:
public class UserProfile
{
public virtual string Id { get; set; }
public virtual string Interests { get; set; }
public virtual string Biography { get; set; }
public virtual string SmokingAttitude { get; set; }
public virtual string DrinkingAttitude { get; set; }
public virtual string Projects { get; set; }
public virtual string Groups { get; set; }
}
视图模型:
public class UserProfileViewModel
{
[Display(Name = "Interests")]
public string Interests { get; set; }
[Display(Name = "Biography/Comments")]
public string Biography { get; set; }
[Display(Name = "Attitude to Smoking")]
public string SmokingAttitude { get; set; }
[Display(Name = "Attitude to Drinking")]
public string DrinkingAttitude { get; set; }
[Display(Name = "Projects working on")]
public string Projects { get; set; }
[Display(Name = "Groups joined")]
public string Groups { get; set; }
public IEnumerable<SelectListItem> AttitudeList
{
get
{
return new[]{
new SelectListItem {Value = "not specified", Text = "not specified"},
new SelectListItem {Value = "very negative", Text = "very negative"},
new SelectListItem {Value = "negative", Text = "negative"},
new SelectListItem {Value = "compromisable", Text = "compromisable"},
new SelectListItem {Value = "neutral", Text = "neutral"},
new SelectListItem {Value = "positive", Text = "positive"},
};
}
}
}
如您所见,ID 属性存储在用户配置文件模型中(作为用户 table 的键),但不在用户配置文件视图模型中。不过,用户配置文件视图模型有一个附加字段 AttitudeList 定义用于 selection/dropdown 列表操作。我认为这是自动映射器无法正常工作的原因,但我完全不知道如何解决这个问题。
有人可以帮忙吗?这个想法只是从模型 to/from 视图模型映射公共字段,同时忽略不同的字段。我希望我不必手动映射它们并编写数十行代码...
忽略那个属性。
Mapper.CreateMap<UserProfile, UserProfileViewModel>()
.Ignore(dst => dst.AttitudeList);
Ignore mapping one property with Automapper
好吧,我正在尝试映射我的用户配置文件模型 to/from 用户配置文件视图模型,但它不会工作,因为它们不共享所有数据字段:
型号:
public class UserProfile
{
public virtual string Id { get; set; }
public virtual string Interests { get; set; }
public virtual string Biography { get; set; }
public virtual string SmokingAttitude { get; set; }
public virtual string DrinkingAttitude { get; set; }
public virtual string Projects { get; set; }
public virtual string Groups { get; set; }
}
视图模型:
public class UserProfileViewModel
{
[Display(Name = "Interests")]
public string Interests { get; set; }
[Display(Name = "Biography/Comments")]
public string Biography { get; set; }
[Display(Name = "Attitude to Smoking")]
public string SmokingAttitude { get; set; }
[Display(Name = "Attitude to Drinking")]
public string DrinkingAttitude { get; set; }
[Display(Name = "Projects working on")]
public string Projects { get; set; }
[Display(Name = "Groups joined")]
public string Groups { get; set; }
public IEnumerable<SelectListItem> AttitudeList
{
get
{
return new[]{
new SelectListItem {Value = "not specified", Text = "not specified"},
new SelectListItem {Value = "very negative", Text = "very negative"},
new SelectListItem {Value = "negative", Text = "negative"},
new SelectListItem {Value = "compromisable", Text = "compromisable"},
new SelectListItem {Value = "neutral", Text = "neutral"},
new SelectListItem {Value = "positive", Text = "positive"},
};
}
}
}
如您所见,ID 属性存储在用户配置文件模型中(作为用户 table 的键),但不在用户配置文件视图模型中。不过,用户配置文件视图模型有一个附加字段 AttitudeList 定义用于 selection/dropdown 列表操作。我认为这是自动映射器无法正常工作的原因,但我完全不知道如何解决这个问题。
有人可以帮忙吗?这个想法只是从模型 to/from 视图模型映射公共字段,同时忽略不同的字段。我希望我不必手动映射它们并编写数十行代码...
忽略那个属性。
Mapper.CreateMap<UserProfile, UserProfileViewModel>()
.Ignore(dst => dst.AttitudeList);
Ignore mapping one property with Automapper