Application can't scaffold items

44,867

Solution 1

I had this problem too, I solved the problem by calling the base.onModelCreating in my DB context

base.OnModelCreating(modelBuilder);

Solution 2

In my case I moved my connection strings out of the Web.config to

<connectionStrings configSource="ConnectionStrings.config"/>

that when I started getting the error when I was trying to scaffold.

There was an error running the selected code generator: ‘Exception has been thrown by the target of an invocation.’

Moving my connection strings back to the Web.config solved my issue.

Solution 3

I had faced the same problem while creating controller using scaffold with 'ASP.NET MVC5 using views with Entity Framework'

The problem was because I provided <connectionStrings> tag before <configSections> in web.config. setting <connectionStrings> after <configSections> resolved the issue.

I guess, while scaffolding, ASP.NET MVC want to resolve the Entity Framework first, then the connection string, as I have provided connection string earlier so that after resolving the Entity Framework version it could not find the connection string so it thrown invocation problem.

Solution 4

This solved the issue for me,

Adding throwIfV1Schema: false to base of DbContext

As so:

public MyDbContext() : base("ConectionStringName", throwIfV1Schema: false) { }

Solution 5

Late reply; but, I am posting this answers with the hope that somebody can use this answer to solve their problem.

For some reason, if your program CANNOT read connectionString information (either from web.config or from other means) then, this error will be thrown.

Make sure that valid connectionString information is being retrieved properly without any problem.

Share:
44,867
runamuk0
Author by

runamuk0

Ex-Commercial Pilot, Ex-Flight Instructor

Updated on July 09, 2022

Comments

  • runamuk0
    runamuk0 almost 2 years

    I created an MVC 5 application in VS 2013 Professional and then used EF 6.1 code first with an existing DB on SQL Server Express. When I try to create the views I’m using the “New scaffolded item…” then selecting the “MVC 5 controller with views, using Entity Framework.” I select the model and context classes and click OK. Then the following error message appears and no code is created. I’ve uninstalled EF Power Tools with the same error.

    Error

    There was an error running the selected code generator: ‘Exception has been thrown by the target of an invocation.’

    I've also tried uninstalling/reinstalling VS 2013 and SQL Server with no changes.

    Any other ideas about what might cause this error?

  • sam starobin
    sam starobin about 9 years
    this worked for me to, must be some configuration change it makes (but where, in the solution or one of the config files?)
  • Enzero
    Enzero almost 9 years
    @JosephCasey I don't know but you must have some kind of dependency somewhere which is stopping you from uninstalling check what packages you have installed and see what dependencies they have. You will have to uninstall them first before removing EntityFramework.
  • Enzero
    Enzero almost 9 years
    @JosephCasey are you by chance using the beta version of Entity Frameworks since that does have a dependency (EntityFramework.Commands). It might be better to just use the current version at date being 6.1.3.
  • Joseph Casey
    Joseph Casey almost 9 years
    I have EF 6.1.3. I can try to check it later. I think the bigger problem was my out-of-date approach.
  • Aaron
    Aaron almost 9 years
    This fixed the problem for me. Put this line of code at the very end of the OnModelCreating method after all the entities have bee configured. VS2013 Update 4
  • David McSpadden
    David McSpadden over 8 years
    had duplicate connection string entries in my web.config
  • Gabriel Sadaka
    Gabriel Sadaka over 8 years
    I had the exact same problem, for some reason it doesn't like the EntityTypeConfiguration class, thanks!
  • Hemant
    Hemant over 8 years
    I had declared the connection strings before the configSections node. So the scaffolder was not able to find EF.
  • Stack0verflow
    Stack0verflow over 8 years
    I have this line in my code to begin with, but I am still having the error when adding new scaffolded items. Using VS 2013 Update 5 and SQL Server Express 2012 and following the tutorial at asp.net/mvc/overview/getting-started/…
  • Andy V
    Andy V about 8 years
    BOOOO MSFT. This was my issue.
  • FuckStackoverflow
    FuckStackoverflow almost 8 years
    I often pride myself on not doing stupid things. This, today, was one of a few. Good catch and call.
  • lordstyx
    lordstyx about 7 years
    Don't forget to rebuild after doing this!
  • Jackson Lopes
    Jackson Lopes almost 7 years
    I too had the connectionstring before the ConfigSections. Moving it after the ConfigSections solved it for me. Cheers !!!
  • Phi
    Phi over 6 years
    @DavidMcSpadden bro, you sould post your comment as a new response. After hours I solved my error because of you. Thanks!
  • Tsagana Nokhaeva
    Tsagana Nokhaeva about 6 years
    Yeah, that was it! Thanks! But how to separate web.config into 2 files without this pain?
  • Tahir Hassan
    Tahir Hassan almost 6 years
    I have my context and model classes in a separate project and this worked for me too. Thanks.
  • Eric Sassaman
    Eric Sassaman over 5 years
    The simple advice "check relationships" led me in the right direction. In my case I had a bad [ForeignKey()] decorator on an EF class. Once that was fixed, problem solved.
  • yibe
    yibe over 5 years
    To me, your answer before your two updates solved the issue. I just used double backslashes (\\) in the place where single backslash (\) was used, and it finally worked thank you.
  • Avinash Barnwal
    Avinash Barnwal almost 4 years
    This Solved the problem for me in VS 2019 ASP.NET (Full Framework) MVC Application. Using Entityframework 6.