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:
@mjwills: "Does a breakpoint on "return locationItems" hit? - Nope, it occurs on the line with .ToList()
@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.