外国Id属性是否应该从Model映射到Dto?

时间:2015-02-04 09:57:54

标签: c# entity-framework automapper graphdiff

如果我有以下型号:

public class Customer
{
    public int Id {get; set;}
    public int CustomerTypeId {get; set;}
    public virtual CustomerType {get; set;}
}

Dto是否应该将外国ID排除在外:

public class CustomerDto
{
    public int Id {get; set;}
    public virtual CustomerType {get; set;}
}

使用Graphdiff更新对象图时,EF是否知道CustomerType映射到CustomerTypeId?

1 个答案:

答案 0 :(得分:0)

是的,您需要使用它,但您可以避免虚拟成员声明。如果您使用AutoMapper,则映射将自动完成。所以,你的Dto看起来像这样:

public class CustomerDto
{
    public int Id {get; set;}
    public int CustomerTypeId {get; set;}
}

映射:

Mapper.CreateMap<Customer, CustomerDto>();
Mapper.CreateMap<CustomerDto, Customer>();