views:

33

answers:

2

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 using automapping or fluent's normal mapping).

Thanks

+1  A: 

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.

Matthew Talbert
Yeah, I've thought about this but I wanted to see if there was a better solution. Isuru's seems to do what I wanted in the first place, thanks though!
Fred
+2  A: 

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

isuruceanu
Aaah, I've read about references but I wasn't sure how to use them. That hit the spot! Thanks!
Fred