Entity framework finding foreign keys

前端 未结 1 896
半阙折子戏
半阙折子戏 2021-01-03 05:47

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

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

var         


        
相关标签:
1条回答
  • 2021-01-03 06:35

    I worked out the 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();
    
    0 讨论(0)
提交回复
热议问题