Ask a questionAsk a question
 

AnswerMapping a collection-derived entity

  • Saturday, November 07, 2009 7:04 PMVRSki2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Being new to the EF, I couldn't even search old posts for the solution -- I have no idea what this technique may be called. Nevertheless, here's what I got.

    The data model has 2 tables -- Contacts and PhoneNumbers with a 1-to-many relationship:
     Contacts
     - ContactId
     - Name
     
     PhoneNumbers
     - PhoneNumberId
     - ContactId
     - PhoneNumber

    Naturally, I'd like to have Contact and PhoneNumber entities. The straightforward approach, as I understand, is to have a Contact class containing a reference to a collection of PhoneNumber(s):

    class Contact
    {
       public PhoneNumber [] PhoneNumbers {get;set;}
    }

    Is it possible to somehow replace PhoneNumber []  with MyPhoneNumbersCollection? I would like to have some logic implemented that deals with phone numbers in the context of a given Contact (for instance, find all phone numbers that satisfy some rules, etc.). So, ultimately, the domain model would contain a 3rd entity (is it even appropriate to call it an entity) called MyPhoneNumbersCollection, and the Contact class would be:

    class Contact
    {
       public MyPhoneNumbersCollection PhoneNumbers {get;set;}
    }

    Any help / suggestions are greatly appreciated.

    Regards, VRSki

Answers

  • Sunday, November 08, 2009 2:27 PMIdo Flatow. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    In EF3.5 you cannot do this because EF 3.5 doesn't support POCO, meaning the designer builds the entity classes for you. In the case of collections the generated type is EntityCollection<T>.

    You can perhaps create extension methods for the EntityCollection type that will work on specific generic types, for example:

    public static bool IsPhoneExists(this EntityCollection<PhoneNumber> phones, PhoneNumber targetPhone)
    {
    ...
    }

    And call it by:
    Contact c;
    c.PhoneNumbers.IsPhoneExists(p);

    In EF 4 you can build POCO classes but I'm unsure to whether you can change the type of collection (if so, it will only be by editing the template). You might be able to do it easly if you work with code-only mode. You'd better check with the EF4 forum if it interests you
    http://social.msdn.microsoft.com/Forums/en-US/adonetefx


    Please mark posts as answers/helpful if it answers your question
    • Marked As Answer byVRSki2 Sunday, November 08, 2009 4:29 PM
    •  

All Replies

  • Sunday, November 08, 2009 2:27 PMIdo Flatow. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    In EF3.5 you cannot do this because EF 3.5 doesn't support POCO, meaning the designer builds the entity classes for you. In the case of collections the generated type is EntityCollection<T>.

    You can perhaps create extension methods for the EntityCollection type that will work on specific generic types, for example:

    public static bool IsPhoneExists(this EntityCollection<PhoneNumber> phones, PhoneNumber targetPhone)
    {
    ...
    }

    And call it by:
    Contact c;
    c.PhoneNumbers.IsPhoneExists(p);

    In EF 4 you can build POCO classes but I'm unsure to whether you can change the type of collection (if so, it will only be by editing the template). You might be able to do it easly if you work with code-only mode. You'd better check with the EF4 forum if it interests you
    http://social.msdn.microsoft.com/Forums/en-US/adonetefx


    Please mark posts as answers/helpful if it answers your question
    • Marked As Answer byVRSki2 Sunday, November 08, 2009 4:29 PM
    •  
  • Sunday, November 08, 2009 4:30 PMVRSki2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you.
    Regards, VRSki