简体   繁体   English

在实体框架中按类型获取实体

[英]Get entity by type in Entity Framework

I have a type which is stored in an Entity table.我有一个存储在Entity表中的类型。

I want to get all records by type.我想按类型获取所有记录。

My method like this :我的方法是这样的:

public void read(Type tablename)
{
    // read data in context by type
}

I use this code but it doesn't work :我使用此代码但它不起作用:

var test = new CmsContext().Set(_tableName);

Query will not execute until you use it's results, for example, by enumerating them to a list. Query 在您使用它的结果之前不会执行,例如,通过将它们枚举到列表中。

using System.Linq;

...

using(var ctx = new CmsContext())
{
    var allEntitiesInSet = ctx.Set(_tableName).ToList();
}

You can read about "lazy execution" of queries here: https://msdn.microsoft.com/library/bb738633(v=vs.100).aspx您可以在此处阅读有关“延迟执行”查询的信息: https : //msdn.microsoft.com/library/bb738633(v=vs.100).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM