1

I am currently working with a database that pulls information from another system into it for manipulation. I decided to use Entity Framework and the DB first approach to comprise my data access. One of the unique things about the project is that I am using the buisness key from the external system as the key that will show up in associated tables but it isn't the primary key in my system. I use this key for the relationships in the associated tables EG MyTable.ExternalId --> Review.ExternalId (ExternalId IS NOT the pk in parent table). Because of this, EF doesn't recongize the relationship even though I have a foreign key constraint on the child table making this relationship known in the database.

Is there a way to override EF to tell it to use the ExternalId instead of the PK to build the relationship? Or would it be suggested that I just change the relationship to use the PK of the parent table?

2
  • 1
    You can add a new navigation property by right-clicking your entities and selecting "Add new navigation-property." Then, in the properties window, you select the foreign key you need to navigate to in the select box. Does this approach work with foreign keys that do not point to primary keys? i do consider it bad practice to create entities based on foreign keys that arent primaries. You should just use queries to get those instead of navigational properties in my opinion. Commented Apr 2, 2013 at 13:39
  • @jrosseel The designer shows the foreign keys, but it doesn't show keys that aren't a pk in another table. The ExternalId does have a unique index on it. I do see what you are saying about the query though. Commented Apr 2, 2013 at 13:46

1 Answer 1

1

EF doesn't support unique keys yet so you cannot use your ExternalId as identification of principal record in the database. You either have to use ExternalId as PK in your table or build relation with the real PK of the principal table.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.