开发者

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:

Person
PersonId
Firstname
Lastname

How can I map these two columns to create a person object? (either usin开发者_运维问答g automapping or fluent's normal mapping).

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 :)

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

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 :)


Well, what I would do would be to create a view called "Person" that contains the data you want and then map that, just as you would a normal table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜