C# 如何在Automapper中忽略源对象

C# 如何在Automapper中忽略源对象,c#,automapper,C#,Automapper,这是我的源代码类 public class Content :IAggregateRoot { public Guid Id { get; set; } public string HeaderImage { get; set; } public string AboutText { get; set; } public string Address { get; set; } public long Pho

这是我的源代码类

public class Content :IAggregateRoot
    {
        public Guid Id { get; set; }
        public string HeaderImage { get; set; }
        public string AboutText { get; set; }
        public string Address { get; set; }
        public long Phone { get; set; }
        public long Mobile { get; set; }
        public DateTime CreationTime { get; set; }
    }
这是我的目的地班

public class AboutViewModel
    {
        public string AboutText { get; set; }
        public string Address { get; set; }
        public long Phone { get; set; }
        public long Mobile { get; set; }
    }
我只想忽略source的额外属性,并使用Automapper使用此方法将source映射到destination

public static AboutViewModel ConvertToAboutViewModel(this Content content)
        {
           // Mapper.CreateMap<Content,AboutViewModel>().ForMember(x=>x.AboutText,)
            return Mapper.Map<Content, AboutViewModel>(content);
        }
public static AboutViewModel转换为boutviewmodel(此内容)
{
//Mapper.CreateMap().FormMember(x=>x.AboutText,)
返回Mapper.Map(内容);
}

如何做到这一点???

Automapper默认情况下会忽略这些属性,因为它们在目标类中不存在

你真的试过运行你的代码吗?如果它不起作用,请您发布错误或异常的详细信息

您还需要这样定义基本地图:

 Mapper.CreateMap<Content, AboutViewModel>();
 Mapper.CreateMap<AboutViewModel, Content>();
Mapper.CreateMap();
CreateMap();

请确保在调用ConvertToAboutViewModel之前已运行上述代码

是的,如果运行了,我将非常高兴!引发了类型为“AutoMapper.AutoMappingException”的异常。请检查上面的我的修订版,需要设置映射,确保在尝试调用Mapper.Map之前运行了上述代码