0

我是 automapper 的新手,我遇到了问题。在这种情况下,自动映射器用于将模型(生成的实体框架)映射到我自己的视图模型。这就是发生的情况,源模型及其值被映射到目标模型,但目标值始终为空。价值观怎么了?

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

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 包含目标对象,但始终为空:

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);
4

1 回答 1

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

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

如果您只想忽略不匹配的属性,请以一种方式查看此答案,否则您将不得不显式映射对象之间的每个属性。

于 2013-08-08T16:36:02.463 回答