首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoMapping嵌套模型

AutoMapping嵌套模型
EN

Stack Overflow用户
提问于 2013-10-24 16:14:30
回答 1查看 1.3K关注 0票数 0

我正在尝试从提供者项目到wcf数据契约的自动机模型。但是,它们都在原始(嵌套)内部有另一个模型/数据契约。例如:我们有一个客户模型,保存诸如姓名、电话号码、EIN等信息.但是,每个客户端可以有多个联系人(另一个模型)。我将如何使用fluent映射在automapper中映射这一点?下面是课程。

DataContracts

客户数据契约

代码语言:javascript
复制
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace DSP.NET.WholeSale.Service.DataContracts
{
    [DataContract]
    public class ClientDataContract
    {
        [DataMember]
        public int? Id { get; set; }

        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string Organization { get; set; }
        [DataMember]
        public string Email { get; set; }

        [DataMember]
        public string UserName { get; set; }
        [DataMember]
        public string Password { get; set; }

        [DataMember]
        public List<ContactDataContract> Contacts { get; set; }

        [DataMember]
        public string WorkPhone { get; set; }
        [DataMember]
        public string HomePhone { get; set; }
        [DataMember]
        public string MobilePhone { get; set; }
        [DataMember]
        public string FaxNumber { get; set; }
        [DataMember]
        public string Language { get; set; }
        [DataMember]
        public string CurrencyCode { get; set; }
        [DataMember]
        public string Notes { get; set; }


        [DataMember]
        public AddressDataContract PrimaryAddress { get; set; }
        //public Address MailingAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }
        //public Address PostalAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }

        [DataMember]
        public AddressDataContract SecondaryAddress { get; set; }
        //public Address BillingAddress { get { return this.SecondaryAddress; } set { this.SecondaryAddress = value; } }

        [DataMember]
        public string VATName { get; set; }
        [DataMember]
        public int? VATNumber { get; set; }
    }
}

接触数据契约

代码语言:javascript
复制
using System.Runtime.Serialization;

namespace DSP.NET.WholeSale.Service.DataContracts
{
    [DataContract]
    public class ContactDataContract
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string Organization { get; set; }

        [DataMember]
        public string Email { get; set; }
        [DataMember]
        public string UserName { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string WorkPhone { get; set; }
        [DataMember]
        public string HomePhone { get; set; }
        [DataMember]
        public string MobilePhone { get; set; }
        [DataMember]
        public string FaxNumber { get; set; }
        // TODO: Language Code also
        // TODO: Currency Code
        [DataMember]
        public string Notes { get; set; }


        [DataMember]
        public AddressDataContract PrimaryAddress { get; set; }
        //public Address MailingAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }
        //public Address PostalAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }

        [DataMember]
        public AddressDataContract SecondaryAddress { get; set; }
        //public Address BillingAddress { get { return this.SecondaryAddress; } set { this.SecondaryAddress = value; } }

        [DataMember]
        public string VATName { get; set; }
        [DataMember]
        public int? VATNumber { get; set; }
    }
}

提供程序模型

客户模型

代码语言:javascript
复制
public class Client
{
    public int? Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Organization { get; set; }
    public string Email { get; set; }

    public string UserName { get; set; }
    public string Password { get; set; }

    public List<Contact> Contacts { get; set; }

    public string WorkPhone { get; set; }
    public string HomePhone { get; set; }
    public string MobilePhone { get; set; }
    public string FaxNumber { get; set; }
    public string Language { get; set; }
    public string CurrencyCode { get; set; }
    public string Notes { get; set; }


    public Address PrimaryAddress { get; set; }
    public Address MailingAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }
    public Address PostalAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }

    public Address SecondaryAddress { get; set; }
    public Address BillingAddress { get { return this.SecondaryAddress; } set { this.SecondaryAddress = value; } }

    public string VATName { get; set; }
    public int? VATNumber { get; set; }
}

接触模型

代码语言:javascript
复制
public class Contact
{
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Organization { get; set; }

    public string Email { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string WorkPhone { get; set; }
    public string HomePhone { get; set; }
    public string MobilePhone { get; set; }
    public string FaxNumber { get; set; }
    // TODO: Language Code also
    // TODO: Currency Code
    public string Notes { get; set; }


    public Address PrimaryAddress { get; set; }
    public Address MailingAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }
    public Address PostalAddress { get { return this.PrimaryAddress; } set { this.PrimaryAddress = value; } }

    public Address SecondaryAddress { get; set; }
    public Address BillingAddress { get { return this.SecondaryAddress; } set { this.SecondaryAddress = value; } }

    public string VATName { get; set; }
    public int? VATNumber { get; set; }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-28 09:50:51

您必须为所有使用的类创建映射,以便将Client映射到ClientDataContract

代码语言:javascript
复制
Mapper.CreateMap<Client, ClientDataContract>(); 
Mapper.CreateMap<Contact, ContactDataContract>();
Mapper.CreateMap<Address, AddressDataContract>();

Wiki页面说,创建ContactDataContract映射就足够了,所有的泛型集合都将被映射。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19571248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档