问题描述
对于我正在使用实体框架的项目,我希望能够枚举给定对象实例的所有导航属性(假设它是由 EF 生成的对象).从那里我想为每个导航属性获取相关的 Id 属性.
For a project I'm working with Entity Framework and I'd like to be able to enumerate all navigational properties for a given object instance (assuming it's an object generated by EF). From there I'd like to get the related Id property for every navigational property.
例如,如果我得到一个类 Person
的实例,我希望能够找到它的导航属性,称为 Address
和 Boss
.对于这两个导航属性,我想查找"名为 AddressId
和 BossId
的相关 Id 属性.
For example, if I get an instance of the class Person
, I want to be able to find it's navigational properties called Address
and Boss
. For those two navigational properties I want to then "lookup" the related Id properties called AddressId
and BossId
.
我需要这些 Id 属性,以便我可以在不同的数据库上运行查询,该数据库没有相同的外键但确实具有完全相同的 Id.
I need those Id properties so I can run queries on a different database which does not have the same foreign keys but does have exactly the same Ids.
到目前为止,我已经找到了一种方法来获取 RelationshipManager 用于 EF 生成的随机对象实例.在调试时,我可以通过 Manager 的 Relationships
属性获得外键关系.但我只能得到导航属性名称.所以我可以看到有一个 FK_Person_Address
与名为 Address
的导航属性相关,但我找不到 AddressId
.
So far I have figured out a way to get the RelationshipManager for a random object instance generated by EF. And while debugging I can get to the foreign key relations via the Manager's Relationships
property. But I can only get as far as the navigational property name. So I can see there's a FK_Person_Address
which is related to the navigational property called Address
but I can't find the AddressId
.
所以我的问题是,我如何动态地(不了解 Person
类的布局)发现与 Address<相关的
AddressId
属性/代码>?
So my question is, how can I dynamically (with no knowledge of the Person
class' layout) discover the AddressId
property which is related to Address
?
我知道外键关系可能在关系的另一端具有 Id 属性(Boss
指向 Person
而不是 Person
有一个 BossId
).在这种情况下,我仍然希望在检查 Person
的实例时发现 Boss
有一个 PersonId
.
I am aware the Foreign Key relationship might have the Id property on the other side of the relation (Boss
pointing to Person
in stead of Person
having a BossId
). In that case, I'd still like to discover that Boss
has a PersonId
when I'm inspecting an instance of Person
.
推荐答案
这将为您提供一个字典,其中所有导航属性为 Key,所有相关属性为 Value(该值可能是来自其他实体的属性)
This will give you a dictionary with all navigation properties as Key and all related properties as Value (the value might be a property from the other entity)
将这些添加到您的 DBContext 类并调用 db.GetForeignKeyProperties
Add these to your DBContext class and call db.GetForeignKeyProperties<Person>()
结果会是这样的:
地址"-地址 ID"
老板"-Person.BossID"
"Boss" - "Person.BossID"
public Dictionary<string,string> GetForeignKeyProperties<DBType>()
{
EntityType table = GetTableEntityType<DBType>();
Dictionary<string, string> foreignKeys = new Dictionary<string, string>();
foreach (NavigationProperty np in table.NavigationProperties)
{
var association = (np.ToEndMember.DeclaringType as AssociationType);
var constraint = association.ReferentialConstraints.FirstOrDefault();
if (constraint != null && constraint.ToRole.GetEntityType() == table)
foreignKeys.Add(np.Name, constraint.ToProperties.First().Name);
if (constraint != null && constraint.FromRole.GetEntityType() == table)
foreignKeys.Add(np.Name, constraint.ToProperties.First().DeclaringType.Name+"."+constraint.ToProperties.First().Name);
}
return foreignKeys;
}
private EntityType GetTableEntityType<DBType>()
{
return GetTableEntityType(typeof(DBType));
}
private EntityType GetTableEntityType(Type DBType)
{
ObjectContext objContext = ((IObjectContextAdapter)this).ObjectContext;
MetadataWorkspace workspace = objContext.MetadataWorkspace;
EntityType table = workspace.GetEdmSpaceType((StructuralType)workspace.GetItem<EntityType>(DBType.FullName, DataSpace.OSpace)) as EntityType;
return table;
}
这篇关于如何找到与导航属性相关的 Id 属性或属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!