[英]Fluent NHibernate: Mapping a column with a different name
假設我有一張桌子:
Project
Id
Title
ProjectManagerId
ContactId
ProjectManagerId和ContactId都是來自名為Person的表的ID:
Person
PersonId
Firstname
Lastname
如何映射這兩列以創建人員對象? (使用自動映射或fluent的法線貼圖)。
謝謝
只需創建兩個類即可:
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”的視圖,其中包含所需的數據,然后將其映射,就像普通表一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.