简体   繁体   English

Automapper 在 .net 核心中的反向映射中不起作用

[英]Automapper not working in Reverse Mapping in .net core

I am using .Net core with Entity Framework.我在实体框架中使用 .Net 核心。 Below is my code下面是我的代码

View Model查看模型

public class EmployeeVm
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ContactNo { get; set; }
        public string Email { get; set; }
        public DateTime JoiningDate { get; set; }
        public int BranchId { get; set; }
        public int DepartmentId { get; set; }
    }

POCO Class POCO类

public class employee
    {
        [Key]
        public int id { get; set; } 
        public string name { get; set; }
        public string contact_no { get; set; }
        public string email { get; set; }
        public DateTime joining_date { get; set; }
        public int branch_id { get; set; }
        public int department_id { get; set; }      
    }

Automapper configuration from Startup class来自 Startup 类的 Automapper 配置

public void ConfigureServices(IServiceCollection services)
        {
            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingProfile());
            });

            IMapper mapper = mappingConfig.CreateMapper();
            services.AddSingleton(mapper);
        }

Mapping Profile class映射配置文件类

public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            CreateMap<employee, EmployeeVm>();
            CreateMap<EmployeeVm, employee>();
        }
    }

When I am trying to map View Model properties to POCO class properties using below code it is working fine.当我尝试使用下面的代码将视图模型属性映射到 POCO 类属性时,它工作正常。

//Here I am using constructor injection
private readonly IMapper _mapper;
public EmployeeBl(IMapper mapper)
{
    _mapper = mapper;
}

_mapper.Map<employee>(employeeVm)

But when I am trying to map POCO class ( employee ) properties to View Module ( EmployeeVm ) properties then some properties are not mapping as it contains underscore in POCO class但是当我尝试将 POCO 类(员工)属性映射到视图模块( EmployeeVm )属性时,某些属性没有映射,因为它在 POCO 类中包含下划线

Here is the response of postman这是邮递员的回复

{
    "id": 4,
    "name": "test",
    "contactNo": null,
    "email": "test@gmail.com",
    "joiningDate": "0001-01-01T00:00:00",
    "branchId": 0,
    "departmentId": 0,
}

From above response I am expecting to map contactNo, joiningDate, branchId and departmentId properties with respective value.从上面的响应中,我希望将contactNo、joiningDate、branchId 和 DepartmentId属性映射到各自的值。

https://docs.automapper.org/en/stable/Configuration.html#naming-conventions https://docs.automapper.org/en/stable/Configuration.html#naming-conventions

You can set the source and destination naming conventions您可以设置源和目标命名约定

var configuration = new MapperConfiguration(cfg => {
  cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
  cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
});

This will map the following properties to each other: property_name -> PropertyName这会将以下属性相互映射: property_name -> PropertyName

You can also set this at a per profile level您还可以在每个配置文件级别进行设置

public class OrganizationProfile : Profile
{
  public OrganizationProfile()
  {
    SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
    DestinationMemberNamingConvention = new PascalCaseNamingConvention();
    //Put your CreateMap... Etc.. here
  }
}

AutoMapper doesn't map snake_case to PascalCase automatically. AutoMapper 不会自动将snake_case映射到PascalCase You have to configure naming conventions as described here: https://docs.automapper.org/en/v9.0.0/Configuration.html#naming-conventions您必须按照此处所述配置命名约定: https : //docs.automapper.org/en/v9.0.0/Configuration.html#naming-conventions

Or map the properties one at at a time.或者一次映射一个属性。

However, assuming your "POCO class" is something you need to store in a database using some persistance framework, a better approach would be to configure your persistance tool with knowledge about the snake_cased column names in the database, and let your C# objects conform to C# naming conventions, which dictate that property names should be PascalCase.然而,假设你的“POCO 类”是你需要使用一些持久性框架存储在数据库中的东西,更好的方法是配置你的持久性工具,了解数据库中的蛇形列名,并让你的 C# 对象符合C# 命名约定,它规定属性名称应该是 PascalCase。 This means you can name the properties identically and allow the default AutoMapper config to map your objects.这意味着您可以相同地命名属性并允许默认 AutoMapper 配置映射您的对象。

you can also map the prop which has diff name apart of configuration您还可以映射除了配置之外具有差异名称的道具

Mapper.CreateMap<employee, EmployeeVm>()
    .ForMember(dest => dest.JoiningDate, opt => opt.MapFrom(src => joining_date ));

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

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