简体   繁体   English

DbContext - > DbSet - >缺少Where子句(Entity Framework 6)

[英]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. 如果我尝试自动完成方法,则没有。

VS2013

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: 一些考虑:

  • Either method I just mentioned can take a clause similar to how the where clause works. 我刚才提到的任何一种方法都可以采用类似于where子句如何工作的子句。 This means you can skip the 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.

相关问题 在实体框架中的动态 dbset 中使用 where 子句 - Use where clause in dynamic dbset in Entity Framework 实体框架中DBContext,DBSet &lt;&gt;的引用 - References for DBContext, DBSet<> in Entity Framework 实体框架:Count() 在大型 DbSet 和复杂的 WHERE 子句上非常慢 - Entity Framework: Count() very slow on large DbSet and complex WHERE clause 在实体框架中,您应该在哪里检查用户是否有权在 DbSet/DbContext 中获取或设置数据? - In Entity Framework where should you check if the user has permission to Get or Set the data in DbSet/DbContext? dbcontext 上的实体框架选择子句 - Entity framework select clause on a dbcontext 宣布DBSet <Type> 在DBcontext中 - 实体框架代码优先 - Declaring DBSet<Type> within DBcontext - Entity Framework Code First 首先使用Entity Framework 4代码在DbContext.DbSet中插入等效的InsertOnSubmit - InsertOnSubmit equivalent in DbContext.DbSet using Entity Framework 4 code first 如何在 Entity Framework Core 中使用复数 DbSet 属性名称搭建 DbContext? - How to scaffold DbContext with plural DbSet property names in Entity Framework Core? 使用 xUnit 动态测试所有 Entity Framework Core DbContext DbSet&lt;&gt; 对象 - Dynamically Test All Entity Framework Core DbContext DbSet<> objects with xUnit Entity Framework 6 和 DBContext 或 DBQuery 或 DBSet 或 ObjectQuery 与标量变量 - Entity Framework 6 and DBContext or DBQuery or DBSet or ObjectQuery with scalar variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM