views:

30

answers:

1

I would like to know if it is possible to map a 1-many database relationship as an array of primitives on the parent object class. For example, say I have a database as follows:

TABLE: PERSON
    ID - int

TABLE: PERSON_EMAIL_ADDRESSES
    PERSON_ID - int
    EMAIL_ADDRESS - string

For my Person class, I'd like to have the following:

public class Person 
{
    public int ID { get; set; }
    public string[] EmailAddresses { get; set; }
}

The default Linq to SQL & Entity Framework behavior would be to give me a separate class for the PERSON_EMAIL_ADDRESSES table, and a collection of that type on the Person object...which is what I don't want.

Looks like this can be done with NHibernate as described here, but is there any way to do this with either EF or Linq to SQL?

Thanks in advance, Wayne

+1  A: 

If you want a read-only list, as with your NHibernate sample, you should map as usual and then project:

var q = from p in Context.People
        select new Person // Where this is your Person type above, not a mapped entity type
        {
            ID = p.ID, 
            EmailAddresses = from pea in p.EmailAddresses
                             select pea.EmailAddress
        };

This works in both L2S and L2E. If you want read-write, then you need the ID.

Craig Stuntz
Hi Craig. Thanks for the info. I am only interested in a read-only list, but I was hoping to take care of the complexity via the mapping and not have to worry about it with additional types or projection. It looks like the NHibernate tool can do just, and I was hoping L2S and/or L2E could do something similar. +1 for the suggestion though...
WayneC