none
Multiple foreign key to same table, how to rename navigation properties? RRS feed

  • Question

  • Hi,

    I'm new to Entity Framework and stuck with an issue.

    Let's say I have a "user" table with two FK to "address" table : FK_user_address_billing and FK_user_address_delivery.

    With EF Database First I end up with properties named "Address" and "Address1".

    Searched the web for hours and found only poor workarounds. If I rename the navigation properties in the EDMX schema, it works but easily screwed up on next sync to DB structure changes.

    I've read about Code first option, but this time it becomes a hassle each time the DB structure evolves, having to sync everything by hand, or recreate model with loosing all customisation.

    I cannot think since years this issue hasn't been addressed by EF team, how can we handle this ?

    I tried editing the .tt file to customize the navigation property name, works for classes generation, but then doesn't execute because I have to change the property name also in the EDMX file and I have no way to automate this.

    Please help me ! :) Thanks a lot

    Thursday, November 21, 2019 1:59 PM

All replies

  • With EF Database First I end up with properties named "Address" and "Address1".

    I don't see the problem here, becuase you or anyone else looking at it should know what they mean and the limitation of EF or any ORM.

    I cannot think since years this issue hasn't been addressed by EF team, how can we handle this ?

    IMHO, either you keep doing what you are doing, or you just leave it alone. 

    Maybe, they resolved the issue in EF Core, since it doesn't use the EDMX anymore. 

    • Edited by DA924x Thursday, November 21, 2019 11:30 PM
    Thursday, November 21, 2019 11:27 PM
  • I don't see the problem here, becuase you or anyone else looking at it should know what they mean and the limitation of EF or any ORM.

    The issue is that navigation properties are numbered in alphabetical way. That means, if we later add a Foreign Key that alphabetically sets between the two original ones, it will become Address1, and what previously was Address1 becomes Address2, which screws the whole application.

    I know an ORM can have limitations, but I'm kind of bumped that I find a blocking situation as soon as I start using EF ... This is a common behaviour we have in our team (multiple foreign keys), and it would have a so easy solution of using the Foreign Key name instead of the Entity name that I can't believe this has not been addressed.


    Mike

    Saturday, November 23, 2019 10:19 AM