Fluent NHibernate:使用不同的名称映射列

时间:2010-06-03 04:55:26

标签: c# fluent-nhibernate linq-to-nhibernate

假设我有一张桌子:

Project
Id
Title
ProjectManagerId
ContactId

ProjectManagerId和ContactId都是名为Person:

的表中的id
Person
PersonId
Firstname
Lastname

如何映射这两列以创建人物对象? (使用自动化或流畅的法线贴图)。

由于

2 个答案:

答案 0 :(得分: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; }
}

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

不要忘记约会其他配置:)

答案 1 :(得分:1)

好吧,我要做的是创建一个名为“Person”的视图,其中包含您想要的数据,然后映射它,就像您使用普通表一样。