Automapper将源映射到目标,但dest值始终为null

时间:2013-08-07 09:48:13

标签: entity-framework asp.net-mvc-4 automapper

我是自动播放器的新手,我遇到了问题。在这种情况下,automapper用于将模型(生成的EntityFramework)映射到我自己的viewmodel。发生这种情况,带有值的sourcemodel映射到destinationmodel但dest值始终为null。这些价值观发生了什么?

现在我做了什么: 我将自动化器引用到了我的项目并引导了映射。

public static void RegisterAutoMapperMappings()
        {
            Mapper.Initialize(x =>
            {
                // Add the mappingprofiles you configured below
                x.AddProfile(new RegistrationViewModelProfile());
            });
        }

    public static IMappingExpression<TSource, TDest> IgnoreAllUnmapped<TSource, TDest>(this IMappingExpression<TSource, TDest> expression)
    {
        expression.ForAllMembers(opt => opt.Ignore());
        return expression;
    }

    public class RegistrationViewModelProfile : Profile
    {
        protected override void Configure()
        {
            CreateMap<RegistrationViewModel, contact>().IgnoreAllUnmapped();
            CreateMap<contact, RegistrationViewModel>().IgnoreAllUnmapped();

            CreateMap<RegistrationViewModel, emailaddress>().IgnoreAllUnmapped();
            CreateMap<emailaddress, RegistrationViewModel>().IgnoreAllUnmapped();

            CreateMap<RegistrationViewModel, password>().IgnoreAllUnmapped();
            CreateMap<password, RegistrationViewModel>().IgnoreAllUnmapped();

            //Always check if mapping is valid
            Mapper.AssertConfigurationIsValid();
        }
    }

我的观点模型:

public class RegistrationViewModel
    {
        public HttpPostedFileBase file { get; set; }
        public String EmailAddress { get; set; }
        public String Password { get; set; }
        public string contact_givenname { get; set; }
        public string contact_surname_prefix { get; set; }
        public string contact_surname { get; set; }
        public string contact_gender { get; set; }
        public string contact_country { get; set; }
        public string contact_residence { get; set; }
        public Nullable<DateTime> contact_birth_date{ get; set; }
        public DateTime create_date { get; set; }
        public ICollection<int> Contact_roles { get; set; }
        public string Emailaddress_verificationkey { get; set; }
    }

我的模特:

public partial class contact
    {
        public contact()
        {
            this.contact_connection_rel = new HashSet<contact_connection_rel>();
            this.contact_emailaddress_password_rel = new HashSet<contact_emailaddress_password_rel>();
            this.contact_emailaddress_rel = new HashSet<contact_emailaddress_rel>();
            this.contact_service_role_rel = new HashSet<contact_service_role_rel>();
            this.given_answer = new HashSet<given_answer>();
            this.given_answer1 = new HashSet<given_answer>();
        }

        public int contact_id { get; set; }
        public string contact_initials { get; set; }
        public string contact_givenname { get; set; }
        public string contact_surname_prefix { get; set; }
        public string contact_surname { get; set; }
        public string contact_nickname { get; set; }
        public string contact_gender { get; set; }
        public Nullable<System.DateTime> contact_birth_date { get; set; }
        public string contact_country { get; set; }
        public string contact_residence { get; set; }
        public string contact_ssn { get; set; }
        public Nullable<System.DateTime> create_date { get; set; }
        public Nullable<System.DateTime> modify_date { get; set; }
        public Nullable<System.DateTime> delete_date { get; set; }

        public virtual ICollection<contact_connection_rel> contact_connection_rel { get; set; }
        public virtual ICollection<contact_emailaddress_password_rel> contact_emailaddress_password_rel { get; set; }
        public virtual ICollection<contact_emailaddress_rel> contact_emailaddress_rel { get; set; }
        public virtual ICollection<contact_service_role_rel> contact_service_role_rel { get; set; }
        public virtual ICollection<given_answer> given_answer { get; set; }
        public virtual ICollection<given_answer> given_answer1 { get; set; }
    }

要测试配置,请使用以下行。 vars包含目标对象,但始终为null:

contact c = new contact();
contact testC = unitOfWork.ContactRepository.Find(82);

var x = Mapper.Map<contact, RegistrationViewModel>(testC);
var y = Mapper.Map(regModel, c, typeof(RegistrationViewModel), typeof(contact));
var b = Mapper.DynamicMap<RegistrationViewModel, contact>(regModel);
var z = Mapper.Map<RegistrationViewModel, contact>(regModel, c);
var w = Mapper.Map<RegistrationViewModel, contact>(regModel);

1 个答案:

答案 0 :(得分:1)

expression.ForAllMembers(opt => opt.Ignore());

您告诉AutoMapper忽略所有属性,因此不会映射任何内容。

如果您只想忽略不匹配的属性,请参阅this answer一种方法,否则您必须明确映射对象之间的每个属性。