Reputation: 2560
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
Upvotes: 2
Views: 590
Reputation: 126587
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.
Upvotes: 1