我在asp.net mvc中有一个表单HTML,我想将数据保存为db中的新行,我使用Mapper.Map作为viewmodel和domain本身。 这是确切的错误:
“缺少类型映射配置或不支持的映射。\ r \ n \ r \ nMapping类型:\ r \ nCollateralViewModel - > Collateral \ r \ nViewModels.CollateralViewModel - > Domain.Entities.Collateral \ r \ n \ r \ n \ n目标路径:\ r \ nCollateral \ r \ n \ r \ nSource值:\ r \ nViewModels.CollateralViewModel“}
内部异常:null。
我的配置是:
Mapper.CreateMap<Collateral, CollateralViewModel>();
保存调用,然后保存从控制器创建。
public CollateralViewModel Save(CollateralViewModel model, string username)
{
CollateralViewModel collateral = null;
if (model.Id == 0)
collateral = CreateCollateral(model, username);
else collateral = UpdateCollateral(model, username);
return collateral;
}
private CollateralViewModel CreateCollateral(CollateralViewModel model, string username)
{
var newCollateral = ModelToDomain(model);
}
//this line throws an error
//Mapper.Map<CollateralViewModel, Collateral>(model);
public Collateral ModelToDomain(CollateralViewModel model)
{
Collateral collateral = Mapper.Map<CollateralViewModel, Collateral>(model);
return collateral;
}
但它适用于调用此内容的UPDATE:
public CollateralViewModel DomainToModel(Collateral collateral)
{
CollateralViewModel model = Mapper.Map<Collateral, CollateralViewModel>(collateral);
return model;
}
我也尝试过这样的实例化,但同样的错误:
public Collateral ModelToDomain(CollateralViewModel model)
{
Collateral collateral = new Collateral();
collateral = Mapper.Map<CollateralViewModel, Collateral>(model);
return collateral;
}
我也尝试过这样做,对于不在域中的属性:
Mapper.CreateMap<Collateral, CollateralViewModel>()
.ForMember(x => x.AssetGroupTypes, opt => opt.Ignore())
.ForMember(x => x.Client, opt => opt.Ignore())
.ForMember(x => x.CollateralInstrumentTypes, opt => opt.Ignore())
.ForMember(x => x.CollateralId, opt => opt.Ignore())
.ForMember(x => x.Currencies, opt => opt.Ignore())
.ForMember(x => x.RoundingMethods, opt => opt.Ignore());
有什么我错过的吗?我很擅长使用它。任何帮助?感谢