[英]DbContext -> DbSet -> Where clause is missing (Entity Framework 6)
I've read some tutorials with entity framework 6... 我已经阅读了一些实体框架6的教程......
The basics are easy. 基础很简单。
using (var context = new MyContext())
{
User u = context.Users.Find(1);
}
But how to use "Where" or something else on the "DbSet" with the users? 但是如何在用户的“DbSet”上使用“Where”或其他内容?
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; }
}
And thats the problem which doesnt work. 这就是不起作用的问题。
string username = "Test";
using (var context = new MyContext())
{
User u = from user in context.Users where user.Username == username select user;
}
Error: There was no implementation of the query pattern for source type 'DbSet'. 错误:源类型“DbSet”的查询模式没有实现。 'Where' is not found.
找不到“哪里”。 Maybe a reference or a using directive for 'System.Link' is missing.
可能缺少“System.Link”的引用或using指令。
If i try to autocomplete the methods there are none. 如果我尝试自动完成方法,则没有。
Why it doesnt works? 为什么它不起作用? :(
:(
// Edit: Adding System.Linq to the top of the file changes the functions of the problem above so that i havent a problem anymore. //编辑:将System.Linq添加到文件顶部会更改上述问题的功能,以便我不再有问题。
But why the where
is wrong now? 但为什么现在
where
错了?
The type "System.Linq.IQueryable<User>" cant converted into "User" explicit. There already exists an explicit conversion. (Possibly a cast is missing)
Thanks to @Grant Winney and @Joe. 感谢@Grant Winney和@Joe。
Adding using System.Linq;
using System.Linq;
添加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();
The (second) problem is in what you expect: (第二个)问题出在你的期望中:
User u = from user in context.Users where user.Username == username select user;
You're expecting a single element. 你期待一个元素。 But a
Where
clause returns a list (IEnumerable) of items. 但
Where
子句返回一个列表(IEnumerable)的项目。 Even if there's only one entity that fits the where clause, it will still return a list (with a single item in it). 即使只有一个实体符合where子句,它仍然会返回一个列表(其中包含一个项目)。
If you want a single item, you need to either take the .First()
or the .Single()
item of that list. 如果需要单个项目,则需要使用该列表的
.First()
或.Single()
项。
Some considerations: 一些考虑:
Where
and put the clause straight in this method. Where
并在此方法中直接放置子句。 Single
only works if only one element exists (fitting the clause). Single
仅在只有一个元素存在时才适用(适合该子句)。 If two elements occur, an exception will be thrown. First
works similar to Single
, but it will not throw an exception if multiple items exist (fitting the clause). First
类似于Single
,但如果存在多个项目(适合该子句),它不会抛出异常。 It will simply return the first element it finds (fitting the clause).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.