10

I created a separate Class Library project to store the Database Context and Model Classes. In the same solution, I created an ASP.NET MVC project and referenced the Class Library project, as well as include the Connection String for the Database Context, in the project's Web.config file.

However, when I attempt to add a Controller (with views, using EF), I get the following error:

Exception has been thrown by the target of an invocation.

I am able to see the Database Context and Model Classes in the Add Controller drop down boxes, so I don't think its a referencing issue.

If anyone is also experiencing this error (with this configuration), you assistance will be greatly appreciated.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I had a similar problem when my context contained collections that were accidentally declared with `List` instead of `DbSet`. – AaronLS Jan 04 '15 at 23:17
  • Thanks for the response. I just checked them over and they are all declared using DbSet. The thing is, I have another "Windows Service" project in the same solution, referencing the same Class Library without issue. –  Jan 04 '15 at 23:25
  • Here's a couple cases of similar problems with some things to try. One of those things that could probably be a dozen different problems: http://stackoverflow.com/q/23576294/84206 or http://stackoverflow.com/q/23910162/84206 – AaronLS Jan 04 '15 at 23:42
  • Thanks! Checking them out now... –  Jan 05 '15 at 00:13
  • 1
    Thanks alot Aaron! Found the solution in one of the links you posted (http://stackoverflow.com/a/24154061/3315406). I had more than one ConnectionStrings define. I removed the default and replaced it with the ConstringStrings from my Class Library. Works fine now :D –  Jan 05 '15 at 00:45
  • Glad you found a solution. I'm going to flag this as duplicate and add link to that question. For anyone who might find this question in the future. – AaronLS Jan 05 '15 at 00:52
  • Possible duplicate of [Application can't scaffold items](https://stackoverflow.com/questions/23576294/application-cant-scaffold-items) – krillgar Jul 29 '17 at 01:27
  • @Noximus: Good to see you have fixed this. Do us a favour, create a good answer with the solution and mark as answered. It takes the question off the unanswered list and helps us out when looknig for questions to answer. Good one. – garfbradaz Jul 30 '17 at 16:12

3 Answers3

4

I had more than one ConnectionStrings define. I removed the default and replaced it with the ConstringStrings from my Class Library. Works fine now!

Solution taken from the following post: Application can't scaffold items

  • In my case i changed connection string name parameter from `MyDbContext("connectionString")` to `MyDbContext("name=connectionString")` – sairfan Sep 24 '18 at 20:41
1

Yes, In my case, I had the Configuration block declared twice in the Web.config file. After I removed one the scaffolding worked.

Ayam
  • 41
  • 3
0

I had the same problem until I fixed the version of Microsoft.EntityFrameworkCore to match Microsoft.EntityFrameworkCore.Tools. It was a version mismatch after all!