遍历Entity Framework 6中的表 [英] Looping through tables in Entity Framework 6

查看:110
本文介绍了遍历Entity Framework 6中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这个简单的问题。我是新来的。我有一个包含多个表的实体模型,我想获取表的列表,然后从表中获取列的内容。



<例如,我有一个主题表:生物学,化学和物理,每个表都有一列className(以及其他列)。我想遍历表,获取名称,然后获取该列下的内容,因为我需要 ToList()



我想做这样的事情:

 每个(在myEntityModel中的表)
{
从表
中获取tableName从表
中获得className下的内容}

我尝试使用元数据工作区,并且得到了tableName的列表,但这对我获取每个表的内容没有太大帮助。我可以查询单个表格,但不知道如何更改表格。如果我在myEntity.Biology select {...}中使用r from :,则无法更改我要引用的实体集。



哦,如果在我正在做的事情的帮助下,我试图在Visual Studios 2015中的Ajax Control Toolkit 16.1的帮助下构建一个手风琴。我正在使用Entity Framework 6.0。主题名称将作为headercontainer,并且类别列表将作为gridview添加到手风琴窗格中。我是C#,Visual Studio,数据库,查询,实体模型以及几乎所有正在使用的一切的新手,但是可以随意使用任意多的专业术语。如果有什么令人困惑的地方,我会用Google搜索。谢谢!

解决方案

效率不高,因为它会将整个表加载到内存中,但是您可以这样做:

  var myEntityModel = new MyEntityModel(); //您的上下文

var dbSets = myEntityModel.GetType()。GetProperties()。Where(p => p.PropertyType.Name.StartsWith( DbSet)); //获取Dbset< T>

foreach(dbSets中的var dbSetProps)
{
var dbSet = dbSetProps.GetValue(myEntityModel,null);
var dbSetType = dbSet.GetType()。GetGenericArguments()。First();

var classNameProp = dbSetType.GetProperty( className); //我没有理解,您想要具有className属性的类吗?

if(classNameProp!= null)
{
var contents =((IEnumerable)dbSet).Cast< object>()。ToArray(); //获取内容表
}


sorry for the simple question. I'm new at this. I have an entity model with several tables and I'd like to get a list of the tables, then I'd like to get the contents of a column from the tables.

For example, I have tables of subjects: Biology, Chemistry and Physics, each with a column className (among other columns). I'd like to loop through the tables, get the name then get the contents under that column since I need to ToList() it.

I want to do something like this:

for each (table in myEntityModel)
{
  Get tableName from table 
  Get contents under className from table 
}

I've tried using metadataworkspace, and I got a list of tableName, but that didn't help me too much in getting the contents of each table. I'm able to query individual tables, but I don't know how to change tables once I do. If I use: from r in myEntity.Biology select{...}, I can't change the entityset I'm referring to.

Oh, and, if the context of what I'm doing helps, I'm trying to build an accordion with the help of Ajax Control Toolkit 16.1 in Visual Studios 2015. I'm using Entity Framework 6.0. The subject name is going to be the headercontainer and the list of classes will be added as a gridview into the accordion pane. I'm new to C#, Visual Studios, databases, queries, entity models and almost everything I'm working with, but feel free to use as much jargon as you'd like. I'll google it if something confuses me. Thank you!

解决方案

It's not efficient, because it loads the entire table in memory, but you can do this:

 var myEntityModel = new MyEntityModel(); //your context

 var dbSets = myEntityModel.GetType().GetProperties().Where(p => p.PropertyType.Name.StartsWith("DbSet")); //get Dbset<T>

 foreach (var dbSetProps in dbSets)
 {
     var dbSet = dbSetProps.GetValue(myEntityModel, null);
     var dbSetType = dbSet.GetType().GetGenericArguments().First();

     var classNameProp = dbSetType.GetProperty("className");// i did not undestand, you want classes with className property?

     if (classNameProp != null)
     {
         var contents = ((IEnumerable) dbSet).Cast<object>().ToArray();//Get The Contents of table
     }

这篇关于遍历Entity Framework 6中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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