有什么办法在运行时查看实体框架代码优先的列映射? [英] Is there any way to view Entity Framework Code First's column mappings at runtime?

查看:110
本文介绍了有什么办法在运行时查看实体框架代码优先的列映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个附加在实体框架代码首先,我需要一种方式来获得模型中的列的配置在运行时。例如,这是 OnModelCreating 中的代码设置由 DbModelBuilder

I'm trying to write an add-on to Entity Framework Code First and I need a way to get the configuration of the model columns at run time. For example, this is the code setup on OnModelCreating by the DbModelBuilder:

builder.Entity<NwdEmployee>()
    .Property(n => n.ReportsToID).HasColumnName("ReportsTo");



一旦做到这一点,的EntityFramework知道,我的财产的名字在表中的列名不同,但我怎么能找到字符串上级涉及 ReportsToID 我在运行?理想情况下,我试图写一个方法,如以下内容:

Once this is done, EntityFramework knows that my property's name is different to the column name in the table, but how can I find that the string "ReportsTo" relates to ReportsToID myself at runtime? Ideally, I'm trying to write a method such as a following:

public string GetMappedColumnName<TFrom>(DbContext context, 
    Func<TFrom, object> selector);



哪想被使用:

Which would be used like:

string mappedColumnName = GetMappedColumnName<NwdEmployee>(context, 
    x => x.ReportsToID);



我只是不知道在哪里可以找到的DbContext中映射的列名。难道他们甚至访问?

I just don't know where to find the mapped column names within the DbContext. Are they even accessible?

推荐答案

从理论上是的。实际上我不知道,因为用简单的测试我是不是能够在运行时获得这些信息 - 我看到他们在调试器,但我不能让他们因为我需要使用类型是实体框架内部

Theoretically yes. Practically I'm not sure because with simple test I wasn't able to get those information at runtime - I see them in debugger but I cannot get them because the type I need to use is internal in entity framework.

理论。所有的映射信息,请在运行但不通过反思。它们被保存在背景上肯定是不适合直接使用 MetadataWorkspace 类,因为这个类的每一次互动需要在调试一段时间,花费您了解如何获取数据之前需要。这个数据是无法通过的DbContext API进行访问。您必须转换的DbContext 的ObjectContext 并访问 MetadataWorkspace

The theory. All mapping information are available at runtime but not through reflection. They are stored in the instance on MetadataWorkspace class which was definitely not designed for direct usage because every interaction with this class demands some time spend in debugger before you find how to get data you need. This data are not accessible through DbContext API. You must convert DbContext back to ObjectContext and access the MetadataWorkspace.

ObjectContext objContext = ((IObjectContextAdapter)dbContext).ObjectContext;
GlobalItem storageMapping = objContext.MetadataWorkspace.GetItem<GlobalItem>("NameOfYourContextClass", DataSpace.CSSpace);

现在 storageMapping 是<$ C实例$ C> System.Data.Mapping.StorageEntityContainerMapping 类,这是内部。据我所知,这个类应该是存储和概念模型之间MSL =映射的运行时表示。

Now storageMapping is instance of System.Data.Mapping.StorageEntityContainerMapping class which is internal. As I understand it this class should be runtime representation of MSL = mapping between storage and conceptual model.

如果您使用调试器,您可以探索实例,你会找到更多相关资料性能与列之间的映射(其相当深的嵌套),所以你也可以使用反射来获取他们,但它是在你不拥有,所以任何.NET框架的补丁/修复/更新可以打破你的应用程序类的非公共界面反射。

If you use debugger you can explore the instance and you will find information about mapping between properties and columns (its quite deep nested) so you can also use reflection to get them but it is reflection on non public interface of classes you don't own so any .NET framework patch / fix / update can break your application.

这篇关于有什么办法在运行时查看实体框架代码优先的列映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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