使用 AutoMapper 映射实体
Mapping entity with AutoMapper
我正在尝试使用 auto mapper(ver 6.1.1.0)
映射两个实体
类 我正在测试
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Add1 { get; set; }
public string Add2 { get; set; }
}
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
}
public class Entity
{
public int Id { get; set; }
public string Title { get; set; }
}
我想将 Person 映射到 Employee。我在下面尝试过但不确定如何让它工作
private void MapperTest()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Person, Employee>()
.ForMember(e => e.Address,
p => p.MapFrom(a => a.Address1))
});
var person = new Person { Id = 100, FirstName = "Rob", LastName = "Wood", Add1="Address line 1",Add2 = "Address line 2" ,CountryId =100,Country="UK"};
}
更新,如果 Address 实体包含另一个实体 属性 我在下面尝试过
Mapper.Initialize(cfg => {
cfg.CreateMap<Person, Address>()
.ForMember(a => a.Address1, opt => opt.MapFrom(p => p.Add1))
.ForMember(a => a.Address2, opt => opt.MapFrom(p => p.Add2));
cfg.CreateMap<Person, Employee>()
.ForMember(e => e.Address,
opt => opt.MapFrom(p => p))
.ForMember(b => b.Address.Country,
opt => opt.MapFrom(br => br));
});
这应该有效:
cfg.CreateMap<Person, Address>()
.ForMember(a => a.Address1, opt => opt.MapFrom(p => p.Add1))
.ForMember(a => a.Address2, opt => opt.MapFrom(p => p.Add2));
cfg.CreateMap<Person, Employee>()
.ForMember(e => e.Address,
opt => opt.MapFrom(p => p));
简短说明:您将映射从 Person
拆分为 2 个实体,并告诉映射器 Address
是 Employee
的子项
试试这个
cfg.CreateMap<Person, Employee>() .ForMember(e => e.Address, p => p.MapFrom(a => new Address{Address1=a.Address1, Address2=a.Address2 }) });
我正在尝试使用 auto mapper(ver 6.1.1.0)
类 我正在测试
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Add1 { get; set; }
public string Add2 { get; set; }
}
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
}
public class Entity
{
public int Id { get; set; }
public string Title { get; set; }
}
我想将 Person 映射到 Employee。我在下面尝试过但不确定如何让它工作
private void MapperTest()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Person, Employee>()
.ForMember(e => e.Address,
p => p.MapFrom(a => a.Address1))
});
var person = new Person { Id = 100, FirstName = "Rob", LastName = "Wood", Add1="Address line 1",Add2 = "Address line 2" ,CountryId =100,Country="UK"};
}
更新,如果 Address 实体包含另一个实体 属性 我在下面尝试过
Mapper.Initialize(cfg => {
cfg.CreateMap<Person, Address>()
.ForMember(a => a.Address1, opt => opt.MapFrom(p => p.Add1))
.ForMember(a => a.Address2, opt => opt.MapFrom(p => p.Add2));
cfg.CreateMap<Person, Employee>()
.ForMember(e => e.Address,
opt => opt.MapFrom(p => p))
.ForMember(b => b.Address.Country,
opt => opt.MapFrom(br => br));
});
这应该有效:
cfg.CreateMap<Person, Address>()
.ForMember(a => a.Address1, opt => opt.MapFrom(p => p.Add1))
.ForMember(a => a.Address2, opt => opt.MapFrom(p => p.Add2));
cfg.CreateMap<Person, Employee>()
.ForMember(e => e.Address,
opt => opt.MapFrom(p => p));
简短说明:您将映射从 Person
拆分为 2 个实体,并告诉映射器 Address
是 Employee
试试这个
cfg.CreateMap<Person, Employee>() .ForMember(e => e.Address, p => p.MapFrom(a => new Address{Address1=a.Address1, Address2=a.Address2 }) });