9

How do I know if a property of an EntityObject is a primary key or a foreign key...

 2 years ago
source link: https://www.codesd.com/item/how-do-i-know-if-a-property-of-an-entityobject-is-a-primary-key-or-a-foreign-key.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How do I know if a property of an EntityObject is a primary key or a foreign key?

advertisements

Suppose that I have a class generated by Entity Framework called Student.

Student has the following properties:

Id int,
Name, string
Age, int
TeacherId int

Suppose further that Id refers to the primary key in SQL that identifies what student a Student object refers to and TeacherId is a foreign key that tells who the student's teacher is.

Suppose I want to write a function which takes any EntityObject (such as this one) as a parameter and returns information about which properties are primary keys and foreign keys.

How can I do this?

If this is not appropriate, then how can Entity Framework tell me which properties are primary and foreign keys?

For now, let's not take into consideration composite key fields.


Looking over the code that is autogenerated, I can see that primitive properties in the generated class have several attributes, and among these is an EdmScalarPropertyAttribute which has a boolean EntityKeyProperty which seems to indicate whether or not the property is a key.

How to read the values of the attributes is described in an article here: http://msdn.microsoft.com/en-us/library/71s1zwct.aspx

I'll bet that I can find a consistent pattern for how foreign keys are handled as well!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK