Andre Taron
Andre Taron

Reputation: 121

EntityFramework returning empty results without errors

I'm learning ASP MVC5 and EntityFramework seems to "silently crash" (I know I'm to blame).

I started a new project from the template and added a table [People] to the template database linked to the default connection string in my webconfig:

<add name="DefaultConnection" 
     connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WorstPeople.WebUI-20141007014802.mdf;Initial 
     Catalog=aspnet-WorstPeople.WebUI-20141108014803;Integrated Security=True" providerName="System.Data.SqlClient" />

I then added a new domain class library project, that includes with a repository pattern? (still learning). I have Ninject and Moq.

My view lists every Person in my database (example: return View(repository.People); ). The view works properly if I bind a Mock repository to my DI. However, if I bind it to my EntityFramework context, it returns an empty list instead of an error (and my database has data).

Questions :

  1. Shouldn't EntityFramework (or something) give me an error when it's unable to connect to my [People] table?

  2. Does attaching a .mdf file prevent IIS from launching and EntityFramework from working properly?

  3. As part of the project template MVC5 creates, is there a "database" that launches IIS, or is it only a database file? I don't know much about IIS or databases.

  4. Out of curiosity: Is it bad to store your domain within the same database as your user? I am trying this out to have only 1 database, but I was wondering.

EDIT :

  1. Do I need to change something with the connection string to make it work with EF and that databse, or am I just having a silent error somewhere I can't find?

I noticed I get the same empty result if I modify my class name to Person2, which leaves me thinking "How do you find an error when there's no way to understand where it lies"

Upvotes: 0

Views: 1280

Answers (2)

Andrei
Andrei

Reputation: 44600

  1. It should
  2. No
  3. If it is localdb (as I can see), you probably have a db file, which lies in your project folder
  4. I do it all the time. If there are no special requirements regarding security and if it is an ordinary website, I don't see any reason why not to use 1 db for domain and the rest of data.

Upvotes: 1

Andre Taron
Andre Taron

Reputation: 121

I found out that changing my Connection's name from "DefaultConnection" to "EFDbContext", which is the same as my derived DbContext object, solves the problem.

I'm guessing Entity Framework works by connection a derived DbContext object with an equally named connection name.

Is there any further insight or tips I should consider on the subject?

Upvotes: 0

Related Questions