dimanche 16 août 2015

How can I find the Id property or properties related to a navigational property?

For a project I'm working with Entity Framework and I'd like to be able to enumerate all navigational properties for a given object instance (assuming it's an object generated by EF). From there I'd like to get the related Id property for every navigational property.

For example, if I get an instance of the class Person, I want to be able to find it's navigational properties called Address and Boss. For those two navigational properties I want to then "lookup" the related Id properties called AddressId and BossId.

I need those Id properties so I can run queries on a different database which does not have the same foreign keys but does have exactly the same Ids.

So far I have figured out a way to get the RelationshipManager for a random object instance generated by EF. And while debugging I can get to the foreign key relations via the Manager's Relationships property. But I can only get as far as the navigational property name. So I can see there's a FK_Person_Address which is related to the navigational property called Address but I can't find the AddressId.

So my question is, how can I dynamically (with no knowledge of the Person class' layout) discover the AddressId property which is related to Address?

I am aware the Foreign Key relationship might have the Id property on the other side of the relation (Boss pointing to Person in stead of Person having a BossId). In that case, I'd still like to discover that Boss has a PersonId when I'm inspecting an instance of Person.





Aucun commentaire:

Enregistrer un commentaire