在运行时获取实体框架映射信息 [英] Get Entity Framework Mapping Info at Runtime

查看:91
本文介绍了在运行时获取实体框架映射信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


有人可以向我展示在实体框架中获取映射(EDM属性 - 数据库字段)信息的代码示例。

我应该使用MetadataWorkspace类但仍无法找到解决方案。

典型情况:我在数据库中有2个表[Organization]和[Person],表[Person]中有[OrganizationId]外键。 EF生成EDM,其中实体[Person]具有名为Organization2的属性。

在运行时我需要全部像Organization2属性(在EDM中的实体Person中)的映射映射到OrganizationId(在数据库中的表[Person]中)。

在我们的应用程序中,我们使用的数据库结构的元数据信息包含数据库表之间的关系(如外键OrganizationId)。然后在C#代码中,我们需要使用EF设计器根据数据库结构生成的适当属性(Organization2)为这些OrganizationId外键设置值。

非常感谢任何代码示例。谢谢。

推荐答案

我不确定这是否能回答你的问题(这可能是我在这里遗漏的东西),但是有关映射的信息不会通过元数据API公开。使用XML解析技术(如XLinq或XmlDocument)解析MSL文件中的必要信息应该相对简单。

为什么需要直接修改数据库?如果您只对操纵外键感兴趣,那么您应该能够使用"Person.OrganizationReference.EntityKey"。它不是单一的值,但应该相对容易创建。

无论如何,如果你能更详细地描述你的场景,它会有很大的帮助。
I'm not sure if this answers your question (there's probably something that I'm missing here), but information about mapping is not exposed through metadata API. It should relatively straightforward to parse the necessary information from the MSL file, using XML parsing technologies, such as XLinq or XmlDocument.

Why do you need to modify the database directly? If you are only interested in manipulating foreign keys, then you should be able to use "Person.OrganizationReference.EntityKey". It's not a single value, but should be relatively easy to create.

In any case, it would help a lot if you could describe your scenario in more detail.


这篇关于在运行时获取实体框架映射信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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