实体框架 - 反思外键和关系/关联
我有一个具有挑战性的问题,我想在实体框架映射上使用反射来查找引用表的所有外键,并且我想要作为外键的列的名称。
根据 另一篇文章,我可以轻松找到通过反射表。但这并没有给我包含外键值的属性或列的名称。
我尝试这样做的原因是我有大量表(近 40 个)引用一个项目表。假设用户在项目表中输入了一个名为“Andrew”的新值,后来管理员注意到这实际上只是现有项目“Andrew”的拼写错误。现在我想找到所有对“Andrew”的引用并将这些引用更改为“Andrew”。我希望有效地执行此操作,因此使用反向导航属性会太慢,因为您必须在修改值之前加载它们。我想做的是能够反映表和列的列表,然后直接向数据库发出更新命令。它看起来像这样:
var command = String.Format("UPDATE [{0}] SET [{1}] = {{1}} WHERE [{1}] = {{0}}; ", fk.FromTableName, fk.FromColumnName);
dataContext.ExecuteStoreCommand(command, new Object[] { oldID, newID });
在 LINQ to SQL 中,这实际上非常简单...对 LINQ 自动生成的代码进行了 20 行反射,我就完成了,但我们最近切换到 EF,我找不到通过 EF 的外键列。
我正在寻找的一个简化示例:如果我有一个名为 Employee 的对象,其中包含一个名为 Manager 的导航属性和 ManagerID 的外键,那么我想知道 Manager 是我的导航属性,底层存储是 ManagerID 属性。我想严格通过反射或元数据来完成此操作,以便我可以从中构建动态查询。
I have a challenging problem where I would like to use reflection on my Entity Framework mapping to find all the foreign keys that reference a table, and I want the names of the columns that are the foreign keys.
According to another post on SO, I can easily find the navigation properties on a table through reflection. But this doesn't give me the name of the property or column that contains the foreign key value.
The reason I'm trying to do this is that I have a large number of tables (nearly 40) that reference one table of items. Let's say a user enters a new value in the items table called "Andew" and later an admin notices it's actually just a typo for the already-existing item "Andrew". Now I want to find all references to "Andew" and change those references to "Andrew". I would prefer to do this effeciently, so using the reverse-navigation properties would be too slow since you have to load the values before modifying them. What I would like to do is be able to reflect a list of tables and columns, then issue update commands directly to the database. It would look something like this:
var command = String.Format("UPDATE [{0}] SET [{1}] = {{1}} WHERE [{1}] = {{0}}; ", fk.FromTableName, fk.FromColumnName);
dataContext.ExecuteStoreCommand(command, new Object[] { oldID, newID });
In LINQ to SQL this was actually pretty easy... 20 lines of reflection on the LINQ auto-generated code and I was done, but we switched to EF recently and I can't find the names of the foreign key columns through EF.
A simplified example of what I'm looking for: if I have an object called Employee with a navigation property called Manager and a foreign key of ManagerID, then I want to know that Manager is my navigation property and the underlying storage is the ManagerID property. I want to do this strictly through reflection or metadata so I can build a dynmaic query from it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用链接问题中的想法找到您感兴趣的
EntityType
后,请注意EntityType
继承自EntityTypeBase
,它有一个属性KeyMembers
,它是参与实体密钥的所有EdmMembers
的集合。每个
EdmMember
都有一个Name
,它将是您要查找的字符串"ManagerID"
。Once you've used the ideas in the linked question to get to the
EntityType
you're interested in, note thatEntityType
inherits fromEntityTypeBase
, which has a propertyKeyMembers
which is a collection of all theEdmMembers
that participate in the entity's key.Each
EdmMember
has aName
which will be the string"ManagerID"
that you seek.只是为了节省时间,我想抛出这不是正确答案,但您可以通过 SQL 中的系统视图执行我所要求的操作。我已经尝试过这个并且它有效,但它困扰着我,我可以通过 LINQ to SQL 轻松获取这些数据,但我根本无法在 EF 中找到它。如果没有其他选择,我将不得不使用以下解决方案。 (但是 EF 必须在内部某个地方拥有这些数据......我只想访问它。)
希望发布这个错误的答案至少对我以外的人有用......(另外,仅供参考,这个解决方案需要更多处理多列 PK 的代码 - 我的都是单列)。
Just to save time, I want to throw out this is not the correct answer, but you can do what I'm asking through system views in SQL. I have tried this and it works, but it botheres me I could get this data so easily through LINQ to SQL, yet I can't find it in EF at all. If there's no alternative I'll have to use the below solution. (But EF has to have this data internally somewhere... I just want access to it.)
Hopefully posting this wrong answer is at least useful to someone other than me... (also, just FYI, this solution needs a lot more code to work with multi-column PKs - mine are all single-column).
问题的答案在另一篇文章中: 读取外键元数据以编程方式使用实体框架 4
请注意,发布者的答案对我不起作用。使用@Ashraf 的答案,效果很好。
The answer to the question is in another post here: Read foreign key metadata programatically with Entity Framework 4
Note the poster's answer didn't work for me. Use @Ashraf's answer, which worked great.