EF6 - Why do all my DbSets throw InvalidOperationException when enumerating them?

c# entity-framework entity-framework-6

Question

I am a bit baffled as to what I am missing in configurations or whatever that puts this rock in my way.

What am I trying to do? My original testcase was just to grab a collection, .ToList() it and then hand that to the API. Your pretty standard EF query.

Code

using (var _dbContext = new FooContext())
            {
                ICollection<Location> locationItems = _dbContext.Locations.ToList();
                return locationItems;
            }

I dont' really feel this is necessary, but this is all it revolves around.

What's my issue?

This is the point where it stops making sense to me and gives me strong indications theres something in the background that I am entirely missing out. This is very likely a very simple configuration block or setting. When I try to .ToList() my Locations, or enumerate them with the debugger (same thing really) I get thrown "InvalidOperationException" on every single collection that my _dbContext knows.

Let me rule out obvious questions, Q&A time.

Q: Is it even connected to your Database?

A: Yup, _dbContext.Database.Exists() returns me a true, inspecting _dbContext.Database with the debugger also gives me plenty of matching information confirming theres contact to the SQL server that I am trying to talk to.

Q: Is there data in your SQL db?

A: Yup, plenty. Checked with SSMS.

Q: Does your DbContext even know those Collections?

A: I believe so?

public class FooContext : DbContext
    {
        public FooContext() : base("name=FooDB")
        {
        }

        public DbSet<Car> Cars { get; set; }
        public DbSet<FooEntry> FooEntries { get; set; }
        public DbSet<ContactDetails> ContactDetails { get; set; }
        public DbSet<Country> Countries { get; set; }
        public DbSet<Equipment> Equipment { get; set; }
        public DbSet<FuelType> FuelTypes { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<Meeting> Meetings { get; set; }
        public DbSet<Option> Options { get; set; }
        public DbSet<Person> Persons { get; set; }
        public DbSet<VehicleType> VehicleTypes { get; set; }

        // force any DateTime fields to be DateTime2
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Properties<DateTime>()
                .Configure(c => c.HasColumnType("datetime2"));
        }

    } 

Also, I should probably point out that I am performing multiple way more complex operations in another Project when I import / seed data from a Sharepoint to the DB. All of those work.

Giving Context as to how my solution structure roughly looks like:

  • Multiple projects with their own defined concerns
  • Foo.EntityFramework contains the FooContext configuration, Migrations, etc, your EF stuff.
  • Foo.DomainClasses are the classes that EntityFramework bases it's entire structure upon
  • Foo.Dao.Impl containing the concrete objects to do CRUD. <-- code is in here
  • Foo.SharepointImporter is a project that uses a EntityFramework _dbContext to do CRUD stuff, all working, does not use Foo.Dao.Impl though.

Let me know if you need more information to make an educated guess what's going wrong here.

Comment requests:

  1. @mjwills: "Does a breakpoint on "return locationItems" hit? - Nope, it occurs on the line with .ToList()

  2. @DavidG: "What is the full Exception?" - {"No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information."} -- I feel a bit stupid now that I didn't dig deeper into that Exception myself, this looks very promising.

1
0
2/26/2019 11:15:21 AM

Popular Answer

The problem was that app.config / web.config did not contain the necessary configuration for EntityFramework (for what reason whatsoever).

As pointed out / confirmed by @er-sho all Projects that utilize EntityFramework need a reference to it. Best performed by doing Install-Package EntityFramework on the specified projects, which should generate the references between projects automatically.


Explanation why this is not a duplicate of No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient' :

Initial state:

+----------------------+----------------------------+
|       Project        | EntityFramework installed? |
+----------------------+----------------------------+
| Foo.WebService       | no                         |
| Foo.Dao.Impl         | no                         |
| Foo.EntityFramework  | yes                        |
+----------------------+----------------------------+

Result:

  • No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file.

  • Dao.Impl is missing EntityFramework configuration.


After adding EntityFramework to my DAO Layer:

+----------------------+----------------------------+
|       Project        | EntityFramework installed? |
+----------------------+----------------------------+
| Foo.WebService       | no                         |
| Foo.Dao.Impl         | yes                        |
| Foo.EntityFramework  | yes                        |
+----------------------+----------------------------+

Result:

  • Same exception as before, which seems weird on first glance.
  • Dao.Impl has a correct configuration now in app / web.config, but since the WebService is the Start Project, it still lacks the necessary configuration aswell.

+----------------------+----------------------------+
|       Project        | EntityFramework installed? |
+----------------------+----------------------------+
| Foo.WebService       | yes                        |
| Foo.Dao.Impl         | yes                        |
| Foo.EntityFramework  | yes                        |
+----------------------+----------------------------+

Result:

  • When Foo.WebService gets launched, it reads app.config, providing EntityFramework with the necessary information it needs.
  • Working code.

I hope that this helps somebody in the future with the same issue.

I thank everybody for their time, that commented and guided me towards the solution.

2
2/26/2019 12:34:13 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow