Entity Base Defining Foreign Keys

I want to find all the foreign keys of an object in my entity model.

I used the following to get a list of all the properties for an object:

var workspace = _entities.MetadataWorkspace; var entityType = workspace.GetItems<EntityType>(DataSpace.CSpace).FirstOrDefault(e => e.Name == tablename); var keyNames = entityType.Members.Select(k => k.Name); 

Is there a way to find only those properties that have associations with foreign keys? And also this property in the reference object with which it is associated?

+4
source share
1 answer

I developed a solution:

 var fk = _entities.MetadataWorkspace.GetItems<AssociationType>(DataSpace.CSpace).Where(a => a.IsForeignKey); //check if the table has any foreign constraints for that column var fkname = fk.Where(x => x.ReferentialConstraints[0].ToRole.Name == tablename).Where(x => x.ReferentialConstraints[0].ToProperties[0].Name == columnname); //Get the corresponding reference entity column name var refcol = fkname.Select(x => x.ReferentialConstraints[0].FromProperties[0].Name).First(); 
+6
source

Source: https://habr.com/ru/post/1415175/


All Articles