2010-06-03 71 views
0

比方說,我有一個表:流利的NHibernate:用不同的名稱映射列

Project 
Id 
Title 
ProjectManagerId 
ContactId 

ProjectManagerId和使用ContactID是從名爲Person表這兩個ID:

Person 
PersonId 
Firstname 
Lastname 

我該如何映射這兩列創建一個人物對象? (使用自動映射或流暢的法線映射)。

感謝

回答

2

只需創建兩個類爲:

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; } 
} 

和項目的映射類,因爲它比人:)更有趣

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); 
    } 
} 

不要忘了約會其他配置:)

+0

Aaah,我已閱讀過有關參考文獻,但我不確定如何使用它們。那就是現場!謝謝! – Allov 2010-06-09 12:47:44

1

嗯,我會做什麼是創建一個名爲「Person」的觀點,即包含所需的數據,然後映射,正如你將一個普通的表。

+0

是的,我想過這個,但我想看看是否有更好的解決方案。首先,Isuru似乎在做我想做的事,不過謝謝! – Allov 2010-06-09 12:48:32