C# Automapper在.net core中的反向映射中不工作
我正在使用带有实体框架的.NETCore。下面是我的代码 视图模型C# Automapper在.net core中的反向映射中不工作,c#,automapper,asp.net-core-webapi,C#,Automapper,Asp.net Core Webapi,我正在使用带有实体框架的.NETCore。下面是我的代码 视图模型 public class EmployeeVm { public int Id { get; set; } public string Name { get; set; } public string ContactNo { get; set; } public string Email { get; set; } public DateTi
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类
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; }
}
启动类中的自动映射配置
public void ConfigureServices(IServiceCollection services)
{
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
}
映射配置文件类
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<employee, EmployeeVm>();
CreateMap<EmployeeVm, employee>();
}
}
从上面的回复中,我希望映射带有相应值的contactNo、joiningDate、branchId和departmentId属性 您还可以映射除了配置之外还有不同名称的道具
Mapper.CreateMap<employee, EmployeeVm>()
.ForMember(dest => dest.JoiningDate, opt => opt.MapFrom(src => joining_date ));
Mapper.CreateMap()
.ForMember(dest=>dest.JoiningDate,opt=>opt.MapFrom(src=>joining_date));
您还可以映射除配置外具有不同名称的道具
Mapper.CreateMap<employee, EmployeeVm>()
.ForMember(dest => dest.JoiningDate, opt => opt.MapFrom(src => joining_date ));
Mapper.CreateMap()
.ForMember(dest=>dest.JoiningDate,opt=>opt.MapFrom(src=>joining_date));
您可以设置源和目标命名约定
var configuration = new MapperConfiguration(cfg => {
cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
});
这将使以下属性相互映射:property\u name
->PropertyName
您还可以在每个配置文件级别设置此设置
public class OrganizationProfile : Profile
{
public OrganizationProfile()
{
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
//Put your CreateMap... Etc.. here
}
}
您可以设置源和目标命名约定
var configuration = new MapperConfiguration(cfg => {
cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
});
这将使以下属性相互映射:property\u name
->PropertyName
您还可以在每个配置文件级别设置此设置
public class OrganizationProfile : Profile
{
public OrganizationProfile()
{
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
//Put your CreateMap... Etc.. here
}
}
自动映射器不会自动将
snake\u case
映射到PascalCase
。您必须按照以下说明配置命名约定:
或者一次映射一个属性
但是,假设您的“POCO类”需要使用某种持久性框架存储在数据库中,更好的方法是配置持久性工具,了解数据库中的蛇形列名称,并让您的C#对象符合C#命名约定,这说明属性名应该是PascalCase。这意味着您可以对属性进行相同的命名,并允许默认的AutoMapper配置映射对象。AutoMapper不会自动将
snake\u case
映射到PascalCase
。您必须按照以下说明配置命名约定:
或者一次映射一个属性
但是,假设您的“POCO类”需要使用某种持久性框架存储在数据库中,更好的方法是配置持久性工具,了解数据库中的蛇形列名称,并让您的C#对象符合C#命名约定,这说明属性名应该是PascalCase。这意味着您可以对属性进行相同的命名,并允许默认的AutoMapper配置映射您的对象。这是因为您的属性名称不同。然后您必须逐个配置映射属性。这是因为您的属性名称不同。然后您必须逐个配置映射属性。谢谢,我的问题通过在概要文件级别设置源和目标命名约定得到解决。非常感谢。@Yasser Shaikh在概要文件级别添加了上述命名约定后,POCO类属性映射到视图模型的属性,但现在视图模型属性没有映射到POCO类属性。我正在尝试实现双向映射(POCO类到视图模型和视图模型到POCO类)。谢谢,我的问题通过将源和目标命名约定放在概要文件级别来解决。非常感谢。@Yasser Shaikh在概要文件级别添加了上述命名约定后,POCO类属性映射到视图模型的属性,但现在视图模型属性没有映射到POCO类属性。我试图实现双向映射(POCO类到视图模型和视图模型到POCO类)。