找不到源类型"System.Data.Entity.DbSet"的查询模式的实现 [英] Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
问题描述
我第一次使用Entity Framework,但似乎无法正常工作.
I'm using Entity Framework for the first time, but it seems not working as expected.
我有此代码:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
public static class QueryClass
{
public static void Query()
{
using (var context = new MyDbEntities())
{
DbSet<MyTable> set = context.Tables;
var query = from val in set select value;
}
}
}
在查询行上(确切地说,"set"变量用红色下划线显示),我得到了错误:
On the query line (exactly the "set" variable is underlined in red) I get the error:
找不到源类型查询模式的实现 'System.Data.Entity.DbSet'.'选择'未找到.缺少参考或 'System.Linq'的using指令
Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'.'Select' not found. Missing a reference or an using directive for 'System.Linq'
MyDbEntities
是由Entity Framework以数据库优先方式自动生成的,context.Tables
是DbSet
,因此它应该能够使用通过using
指令添加的Linq.为了避免造成误解,我在本课程中找到以下内容:
MyDbEntities
is auto-generated by Entity Framework in a Database-First approach, context.Tables
is a DbSet
, so it should be able to use Linq, which has been added through the using
directive. In order to avoid misurderstantings, within this class I find the following:
public virtual DbSet<MyTable> Tables { get; set; }
为了使select
正常工作,我缺少什么?
What am I missing in order to make the select
work?
谢谢.
推荐答案
您将需要添加对System.Data.Linq的引用
you will need to add reference to System.Data.Linq
System.Data.Linq是LINQ-SQL特定的(DataContext等)
System.Data.Linq is LINQ-SQL specific (DataContext, etc)
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Linq;
using System.Linq;
public static class QueryClass
{
public static void Query()
{
using (var context = new MyDbEntities())
{
IQueryable<MyTable> qTable= from t in context.Tables
select t; // can you confirm if your context has Tables or MyTables?
Console.WriteLine("Table Names:");
foreach (var t in qTable)
{
Console.WriteLine(t.Name);//put the relevant property instead of Name
}
}
}
}
这篇关于找不到源类型"System.Data.Entity.DbSet"的查询模式的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!