我是第一次使用 Entity Framework,但它似乎没有按预期工作。
我有这个代码:
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”变量用红色下划线标出)我得到错误:
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
,所以它应该是能够使用 Linq,它已通过 using
指令添加。为了避免误解,在这个类中我发现了以下内容:
public virtual DbSet<MyTable> Tables { get; set; }
为了使 select
正常工作,我缺少什么?
谢谢。
最佳答案
您需要添加对 System.Data.Linq 的引用
System.Data.Linq 是特定于 LINQ-SQL 的(DataContext 等)
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
}
}
}
}
关于c# - 找不到源类型 'System.Data.Entity.DbSet' 的查询模式的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31241541/