简体   繁体   English

实体框架First()不返回任何记录

[英]Entity Framework First() does not return any records

I am having issues with entity framework. 我在实体框架方面遇到问题。 The following code does not return anything: 以下代码不返回任何内容:

Warehouse warehouse = dbcontext.Warehouses.First();

But if I add a break point and mouse over Warehouses I can browse records of the table, but First() doesn't return anything. 但是,如果添加断点并将鼠标悬停在Warehouse上,则可以浏览表的记录,但是First()不会返回任何内容。 In the Immediate Window if I try warehouse.Name I get the error "The name warehouse does not exist in the current context." 如果我尝试使用Warehouse.Name,则在“立即窗口”中将收到错误消息:“名称仓库在当前上下文中不存在。”

The table has around 10,000 records, could this be causing any issues? 该表有大约10,000条记录,这可能会引起任何问题吗?

edit: I have tried: 编辑:我尝试过:

int count = dbcontext.Warehouses.Count();

and the result was 0. This is after I was able to mouse over dbcontext.Warehouses and view records from the table. 结果为0。这是在我能够将鼠标悬停在dbcontext.Warehouses并从表中查看记录之后。 I removed the breakpoing then displayed the result on a webpage and it also displayed 0. So it isn't something I just saw while debugging. 我删除了Breakpoing,然后在网页上显示了结果,并且还显示了0。所以这不是我在调试时看到的东西。

If you are inspecting your variable just below the line where warehouse is declared, and you receive error that warehouse doesn't exist in the current context, it might happen that your debug info is not ok. 如果您在声明仓库的行的下方检查变量,并且收到错误消息,即当前上下文中不存在仓库,则可能发生调试信息不​​正确的情况。 Try to clean/rebuild your project and run Debug configuration. 尝试清理/重建项目并运行“调试”配置。 If you still receive the same error, then you are really out of scope of that variable when you try to inspect it's value. 如果仍然收到相同的错误,则当您尝试检查变量的值时,您实际上超出了该变量的范围。 After your edit: check your connection string, it might happen that you are pointing to a wrong database. 编辑后:检查连接字符串,可能是您指向错误的数据库。 Also, check out source for your entity classes, it might happen that Warehouses is not pointing to a table you think it points to. 另外,请检查您的实体类的源,这可能会导致Warehouses没有指向您认为它指向的表。 Also, have you tried dbcontext.Warehouses.Select(w=>w).First() ? 另外,您是否尝试过dbcontext.Warehouses.Select(w => w).First()?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Entity Framework更新记录而不先加载 - Update records with Entity Framework without loading in first 实体框架不返回null - Entity framework does not return null Entity Framework&Oracle,Linq如果给定函数参数而不是文字,则子句字符串比较不会返回任何记录 - Entity Framework & Oracle, Linq where clause string comparisons do not return any records if given a function parameter rather than a literal Entity Framework Include是否需要.First()? - Does Entity Framework Include needs the .First()? 首先使用实体​​框架代码:删除localdb中的所有记录 - Entity Framework code first : delete all records in localdb .RemoveRange无需先在实体框架中获取数据库记录 - .RemoveRange Without fetching database records first in Entity Framework 代码第一个实体框架Linq语句返回丢失的记录 - Code First Entity Framework Linq Statement Returning Missing Records C# 实体框架:仅更新第一批记录并停止 - C# Entity Framework: Update Only First Batch Records and Stop 如何返回链接到一个 id 实体框架/Razor 的两条记录 - How to return two records linked to one id Entity Framework/Razor 实体框架4:是什么原因导致.Take(10)返回20条记录? - Entity Framework 4: What causes .Take(10) to return 20 records?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM