如何找到Id属性或与导航属性相关的属性? [英] How can I find the Id property or properties related to a navigational property?

查看:86
本文介绍了如何找到Id属性或与导航属性相关的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我正在使用Entity Framework,并且希望能够枚举给定对象实例的所有导航属性(假设它是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的实例,则希望能够找到它的导航属性,称为AddressBoss.然后,对于这两个导航属性,我想查找"名为AddressIdBossId的相关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.

到目前为止,我已经找到一种获取

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.

推荐答案

这将为您提供一个字典,其中所有导航属性都作为键,所有相关属性都作为值(值可能是另一个实体的属性)

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<Person>()

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属性或与导航属性相关的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆