【问题标题】:Should foreign Id properties be mapped from Model to Dto?是否应该将外部 Id 属性从 Model 映射到 Dto?
【发布时间】:2015-02-04 09:57:53
【问题描述】:

如果我有以下型号:

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

D 是否应该排除外国 ID 看起来像这样:

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

并且在使用 Graphdiff 更新对象图时,EF 会知道 CustomerType 映射到 CustomerTypeId 吗?

【问题讨论】:

    标签: c# entity-framework automapper graphdiff


    【解决方案1】:

    是的,您需要使用它,但您可以避免声明虚拟成员。如果您使用AutoMapper,则映射将自动完成。因此,您的 Dto 将如下所示:

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

    还有映射:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-05
      • 1970-01-01
      • 2019-08-18
      • 2021-11-19
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多