阅读外键编程的元数据与实体框架4 [英] Read foreign key metadata programatically with Entity Framework 4
问题描述
有谁知道如何人去有关获取模式的信息产生出实体框架?一个EDMX的
Does anyone know how one goes about obtaining the schema information out of an edmx generated Entity Framework?
具体来说,我要管理遍历了,我目前没有实例的实体的外键,并获得它的外键关系,我想通过反射的方式,将是一般要做到这一点适用于任何实体类没有定制$ C $每次℃。
Specifically I want to manage to traverse the foreign key for an entity that I don't currently have an instance of and get the it's foreign key relationships, and I want to do this via reflection in a way that'll be generically applied to any entity class without custom code each time.
EG:我的架构中有2类,用户和组。我有数字42,我知道从用户实体的GroupId物业来了,但此刻我不知道如何来检测的用户外键这个GroupId的财产它由集团实体的GroupId属性。
EG: My schema has 2 classes, User and Group. I have the number "42" that I know came from the "GroupId" property of a "User" entity, but at the moment I can't work out how to detect that this "GroupId" property of "User" foreign keys to the "Group" entity by it's "GroupId" property.
推荐答案
最终找到了解决这个。相关信息可在上下文中的 RelationshipManager
属性来找到。通过调用 GetAllRelatedEnds()
,然后找到的类型的人 AssociationSet
。
Ended up finding the solution to this. 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.
把所有这些数据,并使用上下文的 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.
这篇关于阅读外键编程的元数据与实体框架4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!