我有2个实体
public class EmployeeEntity : Entity
{
public EmployeeEntity()
{...}
public string FirstName
{...}
public string LastName
{...}
public int DepartmentFK
{...}
}
public class DepartmentEntity : Entity
{
public string Department
{...}
}
第一个包含一个int“ DepartmentFK”,它是我的SQLite数据库文件中的一个外键,但是在我的2个实体中没有链接。两者都有“ Id”列(在SQLite DB中自动生成),“部门”中的“ Id”链接到“ Employees.DepartmentFK”。
对于我的ViewModel,我想创建一个新类
public class Combined : Entity
{
public string FirstName
{...}
public string LastName
{...}
public string Department
{...}
}
并显示
“名字”,“姓氏”和“部门”(基于外键值)。我的Automapper配置如下:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmployeeEntity, Combined>().ForMember(e => e.FirstName, opt => opt.MapFrom(s => s.FirstName))
.ForMember(e => e.LastName, opt => opt.MapFrom(s => s.LastName)).ForAllOtherMembers(d => d.Ignore());
cfg.CreateMap<DepartmentEntity, Combined>().ForMember(d => d.Department, opt => opt.MapFrom(s => s.Department)).ForAllOtherMembers(d => d.Ignore());
});
var mapper = config.CreateMapper();
我可以使用_emp.GetAll().ToObservable()
或_dep.GetAll().ToObservable()
最后,我想将视图绑定到ObservableCollection
public ObservableCollection<Combined> Test
{
get { return _test; }
set { _test = value; }
}
这将使所有员工都加入我的新OC
_test = mapper.Map(_emp.GetAll().ToObservable(), _test);
,但没有部门的相应价值。如何使用自动映射器获得组合版本?