0

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

Allov
  • 1,308
  • 1
  • 13
  • 23

2 Answers2

2

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
  • 1,157
  • 5
  • 23
  • Aaah, I've read about references but I wasn't sure how to use them. That hit the spot! Thanks! – Allov Jun 09 '10 at 12:47
1

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
  • 5,998
  • 35
  • 38
  • 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! – Allov Jun 09 '10 at 12:48