0

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.

beggarboy
  • 173
  • 1
  • 12
  • 3
    What is the full exception message and stack trace? Saying it's just a `InvalidOperationException` doesn't give enough detail. – DavidG Feb 26 '19 at 10:30
  • @DavidG This looks extremely promising, facepalming a bit right now that I didn't dig deeper into the obvious. I am very used that EntityFramework only gives very shallow top level exceptions... – beggarboy Feb 26 '19 at 10:43
  • 2
    Always dig into the error messages, they are there for a reason! – DavidG Feb 26 '19 at 10:43
  • 1
    look like your EF package not installed correctly. kindly reinstall it from package manager console by typing this command => `Install-Package EntityFramework` – er-sho Feb 26 '19 at 10:45
  • Possible duplicate of [No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'](https://stackoverflow.com/questions/18455747/no-entity-framework-provider-found-for-the-ado-net-provider-with-invariant-name) – DavidG Feb 26 '19 at 10:46
  • 1
    Once you reinstall EF the `entityFramework` section will automatically added in web.config. – er-sho Feb 26 '19 at 10:46
  • @er-sho I was under the strong impression I already added EntityFramework to that project, I checked the References, and EntityFramework was there. Regardless, I did `Install-Package EntityFramework` and it did a buncha things, including, as you correctly predicted, fill the app.config with EntityFramework thingsies, that looked pretty dead before. Problem is, I am getting the same error now, I even cleaned and rebuilt that project. – beggarboy Feb 26 '19 at 11:03
  • @er-sho I am actually unsure about something right now. Since all of the described code is happening on a DAL (seperate project) is it sufficient that EntityFramework is installed only there? Atleast from a dependency perspective it makes sense to me that way. But I am not starting the app from that project, but from the WebService project that then delegates the calls down to the DAOs. I am getting the feeling that the app.config for the DAL Project does not have any impact, and that I need my WebService project to have EntityFramework installed aswell. Am I correct with this? – beggarboy Feb 26 '19 at 11:10
  • 1
    Yes you need to install EF on both of your projects, including your DAL and its consumers project too. and make sure same version of EF will be for all – er-sho Feb 26 '19 at 11:13
  • 1
    If using NuGet you don't need to add references of EF to both project. Just the DAL should be enough and NuGet will resolve dependencies of referenced projects. But the config file of the DAL won't get applied into the main project. You will have to add config entries to the main project yourself. – Mohammad Feb 26 '19 at 11:23
  • Also the message from the exception should point what is the problem so can you share that? – Mohammad Feb 26 '19 at 11:23
  • are there any inner exceptions? also you only need to install EF in DAL project or projects which are directly dealing with database and in the main project just add reference to the already installed EF dll file from the DAL project – Fakhar Ahmad Rasul Feb 26 '19 at 11:46
  • @Neptune Installing EntityFramework via NuGet on the WebService aswell fixed it. EntityFramework correctly referenced itself and now it's all smooth as butter. – beggarboy Feb 26 '19 at 11:50
  • @DavidG Personally I would not see this case as a duplicate, but rather a very close sibling to the original problem. – beggarboy Feb 26 '19 at 11:51
  • @DavidG thanks SO for not letting me finish the edit... 2/2. I will try to conclude this in my Answer to this question. If Moderators see this conclusively as the same problem as in the post you linked, I will accept that. But personally it's been the case too many times that duplicates of a question wander off in the wrong direction to be of value. I am saying this because my problem revolved around having multiple projects with EntityFramework being the combining factor, as opposed to just a standalone project. – beggarboy Feb 26 '19 at 11:58
  • Yes, it is the same question. You have exactly the same error message so it must be the same. Granted there may be different solutions, but they should all be posted under the same original post. – DavidG Feb 26 '19 at 13:16

2 Answers2

2

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.

beggarboy
  • 173
  • 1
  • 12
0

You dont need to install entity framework on the Foo.WebService you just need to add a reference of EF on Foo.WebService. Check out this answer and this answer for more details.

Fakhar Ahmad Rasul
  • 1,595
  • 1
  • 19
  • 37
  • As I understood it, when you already have dependencies on other projects, NuGet works that out and basically just references the existing EntityFramework correctly. – beggarboy Feb 28 '19 at 07:32
  • @beggarboy it installs EF, not just reference it. I am working on a solution with pretty same structure and have been through this problem before – Fakhar Ahmad Rasul Feb 28 '19 at 09:26