简体   繁体   中英

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:

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?

Yes, you need to use it but you can avoid virtual member declaration. If you use AutoMapper , then the mapping will be done automatically. So, your Dto will look like this:

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

And the mapping:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM