的DbContext - > DbSet - > Where子句缺失(实体框架6) [英] DbContext -> DbSet -> Where clause is missing (Entity Framework 6)
问题描述
我读过一些教程与实体框架6 ...
I've read some tutorials with entity framework 6...
基础知识很容易。
using (var context = new MyContext())
{
User u = context.Users.Find(1);
}
但如何使用去哪儿或别的东西上了DbSet与用户
But how to use "Where" or something else on the "DbSet" with the users?
public class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")
{
//this.Database.Log = Console.Write;
}
public virtual DbSet<User> Users { get; set; }
}
用户
Users
[Table("User")]
public class User : Base
{
public Guid Id { get; set; }
[StringLength(100)]
public string Username { get; set; }
}
和那它不工作的问题。
string username = "Test";
using (var context = new MyContext())
{
User u = from user in context.Users where user.Username == username select user;
}
错误:有没有查询的实现模式为源类型DbSet。 去哪儿是找不到的。也许引用或using指令System.Link'丢失。
Error: There was no implementation of the query pattern for source type 'DbSet'. 'Where' is not found. Maybe a reference or a using directive for 'System.Link' is missing.
如果我尝试自动完成有没有一个方法。
If i try to autocomplete the methods there are none.
<强>为什么它不工作? (
//编辑:
添加System.Linq的到文件的顶部改变上述问题的功能,使我还没有一个问题了。
// Adding System.Linq to the top of the file changes the functions of the problem above so that i havent a problem anymore.
但是,为什么其中,
现在是错的?
But why the where
is wrong now?
The type "System.Linq.IQueryable<User>" cant converted into "User" explicit. There already exists an explicit conversion. (Possibly a cast is missing)
推荐答案
由于@Grant Winney和@Joe。
Thanks to @Grant Winney and @Joe.
添加使用System.Linq的;
来在那里我特林上面的代码固定的问题文件的命名空间/顶
Adding using System.Linq;
to the namespace/top of the document where i'm tring the code above fixed the problem.
和使用线之上它为列表的第一个项目。
And using the line above it works for the first item of the list.
User user = (select user from context.Users where user.Username == username select user).First();
这篇关于的DbContext - > DbSet - > Where子句缺失(实体框架6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!