C# 如何从ViewModel映射到模型

C# 如何从ViewModel映射到模型,c#,asp.net-mvc,automapper,C#,Asp.net Mvc,Automapper,我有这样的模型和视图模型 public class Estate : BaseEntity { public virtual BaseInformation floorType { get; set; } } public class BaseInformation:BaseEntity { public string Name { get; set; } public virtual BaseInformationHeader M

我有这样的模型和视图模型

public class Estate : BaseEntity
{       
    public virtual BaseInformation floorType { get; set; }     
}

public class BaseInformation:BaseEntity 
{ 
    public string Name { get; set; }     
    public virtual BaseInformationHeader Master { get; set; }
}

public class EstateViewModel : BaseEntityViewModel
{       
    public  long floorType { get; set; }     
}
以及控制器中的代码:

[HttpPost]
public long save(EstateViewModel estateViewModel)
{
    Estate entity = new Estate();
    BaseInformation bi = new BaseInformation();
    bi.id = 1;
    entity.floorType = bi;
    EstateViewModel ev = new EstateViewModel(); 
    Mapper.CreateMap<EstateViewModel, Estate>();
    var model = AutoMapper.Mapper.Map<EstateViewModel,Estate>(estateViewModel);

    return estateRepository.save(entity);
}
[HttpPost]
公共长时间保存(EstateViewModel EstateViewModel)
{
地产实体=新地产();
BaseInformation bi=新的BaseInformation();
bi.id=1;
entity.floorType=bi;
EstateViewModel ev=新的EstateViewModel();
CreateMap();
var模型=AutoMapper.Mapper.Map(estateViewModel);
返回Estatepository.save(实体);
}
执行操作时,AutoMapper会引发以下异常:

发生类型为“AutoMapper.AutoMappingException”的异常 在AutoMapper.dll中,但未在用户代码中处理


发生这种情况的原因是什么?

查看内部异常-它可以很好地描述问题。我还将考虑在静态方法中设置所有的CytMeMAP调用,这些调用在AppStest:/P>
public class AutoMapperConfiguration
{
    public static void Configure()
    {
         Mapper.CreateMap<EstateViewModel, Estate>();          
    }
}
[更新]-要将属性映射到具有不同名称的其他属性:

Mapper.CreateMap<ViewModel, Model>()
                  .ForMember(dest => dest.Id, o => o.MapFrom(src => src.DestinationProp));

您的模型不是很清楚。

我的问题解决方案如下: 代码是这样的:

AutoMapper.Mapper.CreateMap<PersonDTO, Person>()
.ForMember(dest => dest.Address,
           opts => opts.MapFrom(
               src => new Address
               {
                   Street = src.Street,
                   City = src.City,
                   State = src.State,
                   ZipCode = src.ZipCode
               }));
AutoMapper.Mapper.CreateMap()
.FormMember(目的地=>目的地地址,
opts=>opts.MapFrom(
src=>新地址
{
街道=src.街,
城市=src.城市,
State=src.State,
ZipCode=src.ZipCode
}));

内部异常是:缺少类型映射配置或映射不受支持。映射类型:Int64->BaseInformation System.Int64->BonchaghWebApplication.Models.Core.BaseInformation目标路径:Estate.floorType.floorType源值:1 floorType没有要映射到的对象-BaseInformation中有什么?您需要向映射器展示如何处理此问题-向我展示该类中的内容,我将有一个Ganders这是BaseInformation:public类BaseInformation:BaseEntity{public string Name{get;set;}public virtual BaseInformationHeader Master{get;set;}您希望floorType映射到什么?目标类中没有任何内容?你是C#的新手吗?这在动态语言中是可行的,但C#要严格得多——而且您的命名风格暗示了JS的背景。)我想将baseInformation id字段映射到ViewModel中的floortype(很高兴您解决了这个问题:)
Mapper.CreateMap<ViewModel, Model>()
                  .ForMember(dest => dest.Id, o => o.MapFrom(src => src.floorType ));
AutoMapper.Mapper.CreateMap<PersonDTO, Person>()
.ForMember(dest => dest.Address,
           opts => opts.MapFrom(
               src => new Address
               {
                   Street = src.Street,
                   City = src.City,
                   State = src.State,
                   ZipCode = src.ZipCode
               }));