如何从EF CodeFirst DbContext获取dbsets元数据? [英] How to get dbsets metadata from EF CodeFirst DbContext?

查看:223
本文介绍了如何从EF CodeFirst DbContext获取dbsets元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从EF CodeFirst dbcontext中编程获取dbset类的元数据?这是为了循环代码生成目的。

How do you programatically get the metadata for the dbset classes from an EF CodeFirst dbcontext? This is to loop through for code generation purposes.

推荐答案

经过一些额外的研究,我想我找到了一个答案。基本上,您必须下载到ObjectContext,原始的EF上下文,DbContext是一个包装器,并使用下面的MetadataWorkspace信息。

After some additional research, I think I found an answer. Basically, you have to drop down into the ObjectContext, the original EF context that DbContext is a wrapper for, and use the MetadataWorkspace information below.

如果直接从DbContext获取这个直接方法,请添加另一个答案,因为如果有一个更直观,更好的。

Please add another answer if there is a direct way to get this directly from the DbContext as it would be more intuitive and preferable if there is one.

using System.Data.Metadata.Edm;
using System.Data.Objects;
using System.Data.Entity.Infrastructure;

...

using (dbcontext context = new TestContext())
{
   ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext;
   MetadataWorkspace workspace = objContext.MetadataWorkspace;
   IEnumerable<EntityType> tables = workspace.GetItems<EntityType>(DataSpace.SSpace);

}

谢谢,

这篇关于如何从EF CodeFirst DbContext获取dbsets元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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