Home > OS >  Why does my query sends no results but I have data in my db?
Why does my query sends no results but I have data in my db?

Time:12-01

I am learning to build APIs using .NET6 and EF. But I have a strange result when executing my GetAll repository method.

This is what I have :

var pizzas = this.dbContext.Pizzas
                 .Include(pizza => pizza.Sauce)
                 .Include(pizza => pizza.Extras)
                 .AsNoTracking()
                 .AsEnumerable();

return pizzas;

The thing is, when I execute my method in debug mode, I have "Enumeration yielded no results" in my pizzas var. But I do have data in my database. I checked and re-checked, but I do have a pizza in my Pizza table. So, I am a bit confused about why is it sending me an empty enumeration taking in consideration I should have a result ? In addition to that, I do not have conditions in my call, so do you have any idea about what do I need to change ?

Thanks,

CodePudding user response:

Most common issue is what Igor mentioned. Check the connection string at runtime adding

var connectionString = dbContext.Database.GetConnection().ConnectionString;

Then compare that to where you are checking. Often the problem happens if someone checks in a change to the connection string (creating a new local database or pointing at a different DB than you expect) or you've move the DbContext to a separate assembly and updated config files in that assembly but the main application's config still points at an old/invalid location resulting in it going to an old DB or newly created one.

  • Related