假设我有一张桌子:
Project
Id
Title
ProjectManagerId
ContactId
ProjectManagerId 和 ContactId 都是来自名为 Person 的表的 id:
Person
PersonId
Firstname
Lastname
如何映射这两列以创建人员对象?(使用自动映射或流利的法线映射)。
谢谢
假设我有一张桌子:
Project
Id
Title
ProjectManagerId
ContactId
ProjectManagerId 和 ContactId 都是来自名为 Person 的表的 id:
Person
PersonId
Firstname
Lastname
如何映射这两列以创建人员对象?(使用自动映射或流利的法线映射)。
谢谢
只需创建两个类:
public class Person
{
public virtual int PersonId { get; set; }
public virtual string FirstName { get; set; }
public virtual string Surname { get; set; }
}
public class Project
{
public virtual int ProjectId { get; set; }
public virtual string Title { get; set; }
public virtual Person ProjectManager { get; set; }
public virtual Person Contact { get; set; }
}
和 Project 的映射类,因为它比 Person 更有趣 :)
public class ProjectMap : ClassMap<Project>
{
public ProjectMap()
{
Id(x => x.ProjectId);
Map(x => x.Title);
References(x => x.ProjectManager);
References(x => x.Contact);
}
}
如果您使用 FNH
映射覆盖将类似于:
public class ProjectMappingOverride : IAutoMappingOverride<Project>
{
public void Override(AutoMapping<Project> mapping)
{
mapping.Id(x => x.ProjectId); //Usually for Id I have a convention, and not define it here
mapping.Map(x => x.Title); //Also for simple properties. You could remove these lines if you have setup the conventions.
mapping.References(x => x.ProjectManager);
mapping.References(x => x.Contact);
}
}
不要忘记约定其他配置:)
好吧,我要做的是创建一个名为“Person”的视图,其中包含您想要的数据,然后映射它,就像您使用普通表一样。