本文介绍了通过使用Entity框架在函数中传递实体名称来创建函数以获取实体的外键列名。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个函数,它将通过在函数中传递Master和子实体来返回外键属性名称

i want to create a function which will return a foreign key property name by passing Master and child entity in a function

ex- string getForeignKey(MasterEntity me,ChildEntity ce )

ex- string getForeignKey(MasterEntity me,ChildEntity ce)

    return"与MasterEntity主键相关的ChildEntity外键列名";

   return "ChildEntity foreign key column name which is related to MasterEntity Primary key";

}

列表示 - ChildEntity属性名称;

column means- ChildEntity property name;

推荐答案

请尝试以下步骤。

可以在上下文的RelationshipManager属性中找到相关信息。通过调用GetAllRelatedEnds()然后找到AssociationSet类型的那些。

The relevant information can be found in the context's RelationshipManager property. By calling GetAllRelatedEnds() and then finding the ones of the type AssociationSet.

然后,关联集的ElementType包含一个IsForeignKey属性,还有一个数组ReferentialConstraints,它具有每个约束ToRole的属性,分别为ToProperty,FromRole和FromProperty,以及
上的RelationshipMultiplicity,ToRole / FromRole可用于确定外键关系的方向。

The ElementType of the association set then contains an IsForeignKey property and also an array ReferentialConstraints that has properties for each constraint ToRole, ToProperty, FromRole and FromProperty respectively, and the RelationshipMultiplicity on the ToRole/FromRole can be used to determine the direction of the foreign key relationship.

获取所有这些数据并使用context的GetObjectByKey方法我能够以编程方式遍历为上下文定义的外键关系,而不需要关联实体。

Taking all this data and using the context's GetObjectByKey method I was able to programatically traverse the foreign key relationships defined for a context without having the associated entity.

祝你好运,

张龙


这篇关于通过使用Entity框架在函数中传递实体名称来创建函数以获取实体的外键列名。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 22:24