为什么AutoMapper不映射我的子对象?

时间:2016-08-11 16:17:40

标签: c# entity-framework-6 automapper

我有两个实体(表)人和地址。地址是Person的子实体。

public class Person
{
    public Person()
    {
        Addresses = new ObservableCollection<Address>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [DisplayName("Primary Key")]
    [ScaffoldColumn(false)]
    public int Id { get; set; }

    [Required]
    [DisplayName("First Name")]
    [StringLength(25, MinimumLength = 3)]
    public string FirstName { get; set; }

    [DisplayName("Middle Name")]
    [MaxLength(25)]
    public string MiddleName { get; set; }

    [Required]
    [DisplayName("Last Name")]
    [StringLength(25, MinimumLength = 3)]
    public string LastName { get; set; }

    public string FullName => $"{FirstName} {LastName}";

    [Required]
    [DisplayName("Date of Birth")]
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }

    public ObservableCollection<Address> Addresses { get; set; }
}


public class Address
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [DisplayName("Primary Key")]
    [ScaffoldColumn(false)]
    public int Id { get; set; }
    public int OwnerId { get; set; }
    public Person Owner { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}

两个表都有一个(几乎)镜像存档表,分别名为PersonArchive和AddressArchive。

public class PersonArchive
{
    public PersonArchive()
    {
        Addresses = new ObservableCollection<AddressArchive>();
    }

    [Key]
    [DisplayName("Primary Key")]
    [ScaffoldColumn(false)]
    public int Id { get; set; }

    [Required]
    [DisplayName("First Name")]
    [StringLength(25, MinimumLength = 3)]
    public string FirstName { get; set; }

    [DisplayName("Middle Name")]
    [MaxLength(25)]
    public string MiddleName { get; set; }

    [Required]
    [DisplayName("Last Name")]
    [StringLength(25, MinimumLength = 3)]
    public string LastName { get; set; }

    public string FullName => $"{FirstName} {LastName}";

    [Required]
    [DisplayName("Date of Birth")]
    [DataType(DataType.Date)]
    public DateTime BirthDate { get; set; }

    public ObservableCollection<AddressArchive> Addresses { get; set; }
}

public class AddressArchive
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [DisplayName("Primary Key")]
    [ScaffoldColumn(false)]
    public int Id { get; set; }
    public int OwnerId { get; set; }
    public PersonArchive Owner { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}

在Person表中,每个字段都匹配一个类型,除了一个,Addresses字段是Address的集合,PeopleArchive中的相应字段是AddressArchive类型。地址的字段所有者也是如此。

        using (ArchiveDemoContext context = new ArchiveDemoContext())
        {

            //Person p = context.People.Include("Addresses").FirstOrDefault();
            Person p = context.People.FirstOrDefault();

            MapperConfiguration config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Person, PersonArchive>()
                    .ForMember(dest => dest.Addresses, opt => opt.MapFrom(src => src.Addresses));
                cfg.CreateMap<Address, AddressArchive>()
                    .ForMember(dest => dest.Owner, opt => opt.MapFrom(src => src.Owner));
            });

            config.AssertConfigurationIsValid();

            IMapper mapper = config.CreateMapper();

            PersonArchive person = mapper.Map<Person, PersonArchive>(p);
            //context.People.Remove(p);

            context.PeopleArchive.Add(person);

            context.SaveChanges();
        }

调试代码并没有给我任何例外,但只有Person被存档而不是地址。

先谢谢,

1 个答案:

答案 0 :(得分:1)

只要您设置了所有者ID,就无需将地址映射回所有者。更改您的映射:

cfg.CreateMap<Address, AddressArchive>()
      .ForMember(dest => dest.Owner, opt => opt.Ignore());