简体   繁体   中英

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. In the Immediate Window if I try warehouse.Name I get the error "The name warehouse does not exist in the current context."

The table has around 10,000 records, could this be causing any issues?

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. 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.

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. Also, have you tried dbcontext.Warehouses.Select(w=>w).First() ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM