使用 Automapper 映射具有相似名称的成员
Mapping members with similar names Using Automapper
在我只能读取的远程数据库中,一天中的每个小时都有 24 列连续。他们的名字分别是 P1、P2、...、P24。
我必须将这些值复制到我自己的数据库中,我将其命名为 H1、H2、...、H24。
如何使用自动映射器将远程列映射到本地?
CreateMap<Data.Context.SomeTableFromRemoteDb, Data.Entity.MyTableInLocaldb>()
.ForMember(x => x.H1, y => y.MapFrom(z => z.P1))
.ForMember(x => x.H2, y => y.MapFrom(z => z.P2))
.ForMember(x => x.H3, y => y.MapFrom(z => z.P3))
.ForMember(x => x.H4, y => y.MapFrom(z => z.P4))
.ForMember(x => x.H5, y => y.MapFrom(z => z.P5))
.ForMember(x => x.H6, y => y.MapFrom(z => z.P6))
.ForMember(x => x.H7, y => y.MapFrom(z => z.P7))
.ForMember(x => x.H8, y => y.MapFrom(z => z.P8))
.ForMember(x => x.H9, y => y.MapFrom(z => z.P9))
.ForMember(x => x.H10, y => y.MapFrom(z => z.P10))
.ForMember(x => x.H11, y => y.MapFrom(z => z.P11))
.ForMember(x => x.H12, y => y.MapFrom(z => z.P12))
.ForMember(x => x.H13, y => y.MapFrom(z => z.P13))
.ForMember(x => x.H14, y => y.MapFrom(z => z.P14))
.ForMember(x => x.H15, y => y.MapFrom(z => z.P15))
.ForMember(x => x.H16, y => y.MapFrom(z => z.P16))
.ForMember(x => x.H17, y => y.MapFrom(z => z.P17))
.ForMember(x => x.H18, y => y.MapFrom(z => z.P18))
.ForMember(x => x.H19, y => y.MapFrom(z => z.P19))
.ForMember(x => x.H20, y => y.MapFrom(z => z.P20))
.ForMember(x => x.H21, y => y.MapFrom(z => z.P21))
.ForMember(x => x.H22, y => y.MapFrom(z => z.P22))
.ForMember(x => x.H23, y => y.MapFrom(z => z.P23))
.ForMember(x => x.H24, y => y.MapFrom(z => z.P24));
这是当前代码。我想问的是我们可以把它转换成这样吗:
CreateMap<Data.Context.SomeTableFromRemoteDb, Data.Entity.MyTableInLocaldb>()
.ForMember(x => x.ReplaceMemberName(o=> o, "H", "P"), y => y.MapFrom(z => z.P1))
Automapper 有一个名为 replacemembername
的函数,但它似乎不是我要找的。
您对 ReplaceMemberName
的看法几乎是正确的,但使用起来有点不正确。它应该应用于整个映射器配置。有关详细信息,请参阅 https://github.com/AutoMapper/AutoMapper/wiki/Configuration#replacing-characters。
private static void Main(string[] args)
{
var a = new Class1
{
a1 = "1",
a2 = "2",
a3 = "3",
a4 = "4",
a5 = "5"
};
Mapper.Initialize(config =>
{
config.ReplaceMemberName("a", "b");
config.CreateMap<Class1, Class2>();
}
);
var b = Mapper.Map<Class1, Class2>(a);
}
private class Class1
{
public string a1;
public string a2;
public string a3;
public string a4;
public string a5;
}
private class Class2
{
public string b1;
public string b2;
public string b3;
public string b4;
public string b5;
}
您也可以像这样创建本地映射器:
var config = new MapperConfiguration(c =>
{
c.ReplaceMemberName("a", "b");
c.CreateMap<Class1, Class2>();
});
var mapper = config.CreateMapper();
var b = mapper.Map<Class1, Class2>(a);
在我只能读取的远程数据库中,一天中的每个小时都有 24 列连续。他们的名字分别是 P1、P2、...、P24。
我必须将这些值复制到我自己的数据库中,我将其命名为 H1、H2、...、H24。
如何使用自动映射器将远程列映射到本地?
CreateMap<Data.Context.SomeTableFromRemoteDb, Data.Entity.MyTableInLocaldb>()
.ForMember(x => x.H1, y => y.MapFrom(z => z.P1))
.ForMember(x => x.H2, y => y.MapFrom(z => z.P2))
.ForMember(x => x.H3, y => y.MapFrom(z => z.P3))
.ForMember(x => x.H4, y => y.MapFrom(z => z.P4))
.ForMember(x => x.H5, y => y.MapFrom(z => z.P5))
.ForMember(x => x.H6, y => y.MapFrom(z => z.P6))
.ForMember(x => x.H7, y => y.MapFrom(z => z.P7))
.ForMember(x => x.H8, y => y.MapFrom(z => z.P8))
.ForMember(x => x.H9, y => y.MapFrom(z => z.P9))
.ForMember(x => x.H10, y => y.MapFrom(z => z.P10))
.ForMember(x => x.H11, y => y.MapFrom(z => z.P11))
.ForMember(x => x.H12, y => y.MapFrom(z => z.P12))
.ForMember(x => x.H13, y => y.MapFrom(z => z.P13))
.ForMember(x => x.H14, y => y.MapFrom(z => z.P14))
.ForMember(x => x.H15, y => y.MapFrom(z => z.P15))
.ForMember(x => x.H16, y => y.MapFrom(z => z.P16))
.ForMember(x => x.H17, y => y.MapFrom(z => z.P17))
.ForMember(x => x.H18, y => y.MapFrom(z => z.P18))
.ForMember(x => x.H19, y => y.MapFrom(z => z.P19))
.ForMember(x => x.H20, y => y.MapFrom(z => z.P20))
.ForMember(x => x.H21, y => y.MapFrom(z => z.P21))
.ForMember(x => x.H22, y => y.MapFrom(z => z.P22))
.ForMember(x => x.H23, y => y.MapFrom(z => z.P23))
.ForMember(x => x.H24, y => y.MapFrom(z => z.P24));
这是当前代码。我想问的是我们可以把它转换成这样吗:
CreateMap<Data.Context.SomeTableFromRemoteDb, Data.Entity.MyTableInLocaldb>()
.ForMember(x => x.ReplaceMemberName(o=> o, "H", "P"), y => y.MapFrom(z => z.P1))
Automapper 有一个名为 replacemembername
的函数,但它似乎不是我要找的。
您对 ReplaceMemberName
的看法几乎是正确的,但使用起来有点不正确。它应该应用于整个映射器配置。有关详细信息,请参阅 https://github.com/AutoMapper/AutoMapper/wiki/Configuration#replacing-characters。
private static void Main(string[] args)
{
var a = new Class1
{
a1 = "1",
a2 = "2",
a3 = "3",
a4 = "4",
a5 = "5"
};
Mapper.Initialize(config =>
{
config.ReplaceMemberName("a", "b");
config.CreateMap<Class1, Class2>();
}
);
var b = Mapper.Map<Class1, Class2>(a);
}
private class Class1
{
public string a1;
public string a2;
public string a3;
public string a4;
public string a5;
}
private class Class2
{
public string b1;
public string b2;
public string b3;
public string b4;
public string b5;
}
您也可以像这样创建本地映射器:
var config = new MapperConfiguration(c =>
{
c.ReplaceMemberName("a", "b");
c.CreateMap<Class1, Class2>();
});
var mapper = config.CreateMapper();
var b = mapper.Map<Class1, Class2>(a);