lundi 24 avril 2017

How do I get the "NameOf" on a Foreign Key?

I am running into difficulties when trying to extract the attributes of a property contained in a foreign key attribute. To illustrate, I have these properties in some class

[Required(ErrorMessage = "Please enter value.")]
public long ObjectCatalogId{ get; set; }

[ForeignKey(nameof(ObjectCatalogId))]
public ObjectCatalog ObjectCatalog { get; set; }

I want to find a way to get attributes on ObjectCatalogId property by looking at attributes on ObjectCatalog property. I expect this would mean extracting the name of the Foreign Key (which is ObjectCatalogId), then finding that property in the class ObjectCatalogId and ObjectCatalog both live in and using reflection to get the attributes.

My question is how do I get the name on the Foreign Key?

 attribute.GetType().Name

Doesn't work. Additionally, are there more efficient ways of accomplishing this?





Aucun commentaire:

Enregistrer un commentaire