Automapper嵌套集合映射

时间:2020-09-17 07:34:25

标签: c# .net automapper

我有一个有点复杂的类结构,我想使用自动映射器进行映射。我尝试了几件事,但未能成功。

我有一些下面给出的模型类。

    public class PersonModel
    {
        public string FirstName { get; set; }

        public string MiddleName { get; set; }

        public string LastName { get; set; }

        public ICollection<AddressModel> Address { get; set; }
    }

public class AddressModel
{
    public string Line1 { get; set; }

    public string Line2 { get; set; }

    public string Line3 { get; set; }

    public string City { get; set; }

    public StateModel State { get; set; }

    public string PostCode { get; set; }
}

现在我还有一些其他的类,如下所示。

public partial class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<PersonAddress> PersonAddress { get; set; }
}

public partial class PersonAddress
{
    public int Id { get; set; }
    public int? PersonId { get; set; }
    public int? AddressId { get; set; }

    public virtual Address Address { get; set; }
    public virtual Person Person { get; set; }
}

public partial class Address
{
    public int Id { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
    public string City { get; set; }
    public string PostCode { get; set; }
}

它们还有更多嵌套的属性,为简单起见,为了使问题更精确,我将其删除。我想将 PersonModel Person 及其嵌套属性进行映射,并进行反向映射。我正在使用EF Core和.NET Core Web API。

请帮助!

0 个答案:

没有答案