简体   繁体   English

流利的NHibernate:映射具有不同名称的列

[英]Fluent NHibernate: Mapping a column with a different name

Let's say I have a table: 假设我有一张桌子:

Project
Id
Title
ProjectManagerId
ContactId

ProjectManagerId and ContactId are both id's from a table named Person: ProjectManagerId和ContactId都是来自名为Person的表的ID:

Person
PersonId
Firstname
Lastname

How can I map these two columns to create a person object? 如何映射这两列以创建人员对象? (either using automapping or fluent's normal mapping). (使用自动映射或fluent的法线贴图)。

Thanks 谢谢

Just create two classes as: 只需创建两个类即可:

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

and a mapping class for Project as it is more interesting than Person :) 和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);
    }
}

if you are using FNH 如果您正在使用FNH

mapping override will be something like: 映射覆盖将类似于:

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

Do not forget about Convention other Configuration :) 不要忘记约定其他配置:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM