none
Map Navigation Property to a Specific Table RRS feed

  • Question

  • Hi,

    is it somehow possible to map the same entity e.g. Address to different tables for other entities that use the Address as a navigation property?

    To make myself clear, let's say I have two separate entities - Company and Employee; both have the Address navigation property and I'd like to use the same Address type for properties for both, but have two independent tables e.g. CompanyAddresses and EmployeeAddresses in the database and I don't want to have a separate Addresses table, because this wouldn't make sense.

    Thank you


    Clarity VS Precision


    Thursday, June 28, 2012 2:59 PM

Answers

  • Well, you have two ways to bind two tables with the same structure in one entity type (without properties twice)

    • TPC with an abstract base class Address inherited by two "empty" classes: CompanyAddress and EmployeeAddress
    • Horizontal Entity Splitting. In this case, you have to use different keys from the two tables and you need to had a boolean property in your type to identify if the entity is from CompanyAddress table or EmployeeAddress one.

    The issue with TPC is the fact that you need inheritance and the fact that if you use an edmx, you can't use the designer with TPC

    The issue with Horizontal Entity Splitting is the fact that you have the boolean property AND the fact that you have to use unique keys between the two Address tables and the fact that you won't be able to have two navigation properties as expected.

    So I think that you only have one way: using TPC.

    Hope that helps

    Friday, June 29, 2012 9:12 AM

All replies

  • Hi Giorgi Zautashvili,

    Welcome to MSDN Forum.

    Yes, we can split one entity to two database tables. If you are using Entity Data Model, please refer to this walkthrough:

    Walkthrough: Mapping an Entity to Multiple Tables (Entity Data Model Tools)

    If you are using code first, please refer to the section of "Entity Splitting" in this article:

    Code First Mapping Changes in CTP5

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, June 29, 2012 5:35 AM
    Moderator
  • Thanks Allen,

    nice article, though I cannot find solution for my problem there. I just want to have two separate tables for e.g. company addresses and employee addresses using the same Address type. (I don't want to make Address a complex type for some reason)

    Regards


    Clarity VS Precision

    Friday, June 29, 2012 6:55 AM
  • Well, you have two ways to bind two tables with the same structure in one entity type (without properties twice)

    • TPC with an abstract base class Address inherited by two "empty" classes: CompanyAddress and EmployeeAddress
    • Horizontal Entity Splitting. In this case, you have to use different keys from the two tables and you need to had a boolean property in your type to identify if the entity is from CompanyAddress table or EmployeeAddress one.

    The issue with TPC is the fact that you need inheritance and the fact that if you use an edmx, you can't use the designer with TPC

    The issue with Horizontal Entity Splitting is the fact that you have the boolean property AND the fact that you have to use unique keys between the two Address tables and the fact that you won't be able to have two navigation properties as expected.

    So I think that you only have one way: using TPC.

    Hope that helps

    Friday, June 29, 2012 9:12 AM