如何在实体框架6.1中获得所有实体关系的列表

本文关键字:实体 关系 列表 框架 | 更新日期: 2023-09-27 17:53:03

我试图以编程方式获得模型上所有关系"外键"的列表(键,相关对象,外列名)

我发现另一个问题似乎在做同样的事情。但是我无法得到答案中的代码为我工作。

这是我所做的

    public List<string> GetObjectRelations(Type type)
    {
        var metadata = ((IObjectContextAdapter)this.context).ObjectContext.MetadataWorkspace;
        // Get the part of the model that contains info about the actual CLR types
        var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace));
        var fk = metadata.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 == type.Name).Where(x => x.ReferentialConstraints[0].ToProperties[0].Name == type.Name);
        //Get the corresponding reference entity column name
        return fkname.Select(x => x.ReferentialConstraints[0].FromProperties[0].Name).ToList();
    }
下面是我如何调用这个方法
var relations = QueryExtractor.GetObjectRelations(typeof(TSource)); 

但是这个代码不适合我。返回值为空

我如何才能正确地获得外键和它们相关的对象?

下面是我基于muratgu答案的当前代码。但是它现在仍然给我一个关系列表

    public List<Dictionary<string, object>> GetObjectRelations(Type type)
    {
        var relations = new List<Dictionary<string, object>>();
        var metadata = ((IObjectContextAdapter)this.context).ObjectContext.MetadataWorkspace;
        var fk_all = metadata.GetItems<AssociationType>(DataSpace.CSpace).Where(a => a.IsForeignKey);
        var fk_out = fk_all.Where(x => x.ReferentialConstraints[0].ToRole.Name == type.Name).ToList(); // relations going out 
        foreach (var fk in fk_out)
        {
            var relation = new Dictionary<string, object>();
            var fk_ref = fk.ReferentialConstraints[0]; //How can a foreign key relation have more than one column?
            var objectName = fk_ref.FromRole.Name;
            var attributeName = fk_ref.FromProperties[0].Name;
            relation.Add(objectName, attributeName);
            relations.Add(relation);
        }
        return relations;
    }

如何在实体框架6.1中获得所有实体关系的列表

您已经在代码片段中拥有了所需的一切,尽管我有一种预感,返回字符串列表对您来说还不够。我来分析一下,这样你就可以决定怎么处理结果了。

        // assume Child has a foreign key (Parent_Id) to Parent (Id)
        // assume primary keys are not composite
        var type = typeof(Child); 
        var fk_all = metadata.GetItems<AssociationType>(DataSpace.CSpace).Where(a => a.IsForeignKey);
        var fk_out = fk_all.Where(x => x.ReferentialConstraints[0].ToRole.Name == type.Name).ToList(); // relations going out 
        foreach(var fk in fk_out) { // could be many
            var fk_ref = fk.ReferentialConstraints[0]; // what if more than one?
            Console.WriteLine("From {0} - {1} ", fk_ref.FromRole.Name, fk_ref.FromProperties[0].Name);
            Console.WriteLine("To {0} - {1} ", fk_ref.ToRole.Name, fk_ref.ToProperties[0].Name);
        }

假设Child有一个指向Parent (Id)的外键Parent_Id,这将打印:

        From Parent - Id
        To Child - Parent_Id

基于这个答案,你可以使用实体框架映射Api扩展。例如,下面一行检索Product类型的外键:

string[] fks = context.Db<Product>().Fks.Select(fk => fk.PropertyName).ToArray();