简体   繁体   English

是否应将外部ID属性从模型映射到Dto?

[英]Should foreign Id properties be mapped from Model to Dto?

If I have the following model: 如果我有以下模型:

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

Should the Dto exclude foreign Id's to look like this: Dto是否应排除外籍ID如下所示:

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

And when using Graphdiff to update the object graph, will EF know that CustomerType maps to CustomerTypeId? 当使用Graphdiff更新对象图时,EF是否会知道CustomerType映射到CustomerTypeId?

Yes, you need to use it but you can avoid virtual member declaration. 是的,您需要使用它,但是可以避免虚拟成员声明。 If you use AutoMapper , then the mapping will be done automatically. 如果使用AutoMapper ,则映射将自动完成。 So, your Dto will look like this: 因此,您的Dto将如下所示:

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

And the mapping: 和映射:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 确定模型是否应具有外键/导航属性 - Determining if a model should have foreign keys / navigation properties AutoMapper将DTO属性映射到模型列表 - AutoMapper Map DTO Properties to Model List 我应该将外键表存储为Entity Framework中的外键ID还是该表的C#模型? - Should I store foreign key table as foreign key ID or the C# model of that table in Entity Framework? 应该在DTO模型还是在目标实体模型中执行计算? - Should one perform a calculation in a DTO model or in the destination entity model? DTO / POCO应该在所有属性上都有构造函数和私有setter吗? - Should a DTO/POCO have a constructor and private setters on all properties? 将dto映射到实体? 也许自动映射? 属性应该匹配吗? LINQ选择 - mapping dto to entities? maybe automapper? should properties match? linq selection DTO应该由域实体还是持久性生成? - Should a DTO be generated by a domain entity or from persistence? 如何将具有嵌套属性的模型映射到平面Dto? - How do I map a model with nested properties to a flat Dto? 如何将DTO从EF映射到模型 - How to map DTO from EF to Model 从nopCommerce中的产品ID访问基本nop模型自定义属性 - Accessing base nop model custom properties from a product id in nopCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM