.Net Core Dto映射(AutoMapper)
我们假定一个场景, 采用EF Core+Web Api, 这时分可能会呈现EF Core中的Entity Model和在项目中运用的Model之间对应关系呈现偏向, 假如运用属性意逐个对应, 难免会有大量的工作量, 上代码
EntityModels.Employee.cs
public class Employee
{
public Guid Id { get; set; }
public Guid CompanyId { get; set; }
public string EmployeeNo { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Gender Gender { get; set; }
public DateTime DateOfBirth { get; set; }
public Company Company { get; set; }
}
Models.EmployeeDto.cs
public class EmployeeDto
{
public Guid Id { get; set; }
public Guid CompanyId { get; set; }
public string EmployeeNo { get; set; }
public string Name { get; set; }
public string GenderDisplay { get; set; }
public int Age { get; set; }
}
我们能够察看到, 两个实体之间有很多不同的中央, 比方名字拆分啊, 出华诞期和年龄啊等等, 我之前的做法就是硬生生手写代码转换, 但是经过学习大佬们的代码, 发现了一个