1

I have legacy tables with Primary keys. These tables also have surrogate keys that have been used to relate one table to another. I would like to map the Membership.User table (uses a Guid PK) but has a property UserName which holds the surrogate key which relates it to the DepoMembers table. DepoMembers has a numeric PK - but has a surrogate key UserName. UserNames are guaranteed to be unique.

In Linq2SQL - to relate these tables in the designer - we add an association where User.UserName is linked to DepoMembers.UserName - and set the multiplicity of the Association to 1:1. then Modify the Navigation Names to singular.

If we take DepoMembers as the Principal End of the relationship - it should follow that If Membership.User does not exist (As in the case - where a user does not manage the application site but exist in DepoMembers) the Navigation Property User will be Null.

How can this be done in Code First ...

The only alternative I've come up with so far is to create a property DepoMembers.User which uses the UserName Property to retrieve the User using the datacontext.

Ren

Renshai
  • 89
  • 1
  • 6
  • 1
    I think you can't since entity framework does not support navigation properties with columns that are not primary keys. http://stackoverflow.com/questions/15352114/creating-entity-relationship-with-renamed-fields-and-non-primary-key-in-primary/15373090#15373090 – Sérgio S. Filho Mar 25 '14 at 19:46
  • 3
    The only other option you really have is to have Entity Framework treat the surrogate keys as the primary keys, and just have the Guid values be regular columns that are also unique. I'm not sure I recommend that approach...EF might not be the best option for you to use, unfortunately. – Dismissile Mar 25 '14 at 19:51

0 Answers0