Application can't scaffold items

2019-01-06 14:42发布

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?

28条回答
ら.Afraid
2楼-- · 2019-01-06 14:45

In my case, I had to revert my DBContext constructor to just using a static connection string, as defined in web.config

Scaffolding did not work when I used a dynamically created connection string.

public MyContext() : base("name=MyContext") { }
查看更多
趁早两清
3楼-- · 2019-01-06 14:45

My solution was as simple as changing back the connection string name in my Web Config to DefaultConnection. Even though my dbContext has another name!

Took me 2 hours to find out this nonsense!

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-06 14:47

I had the same problem where it wouldn't add scaffold items it seems that uninstalling entity-framework through the nuget package manager console works

To Run Package Manager Console :

Tools -> NuGet Package Manager -> Package Manager Console

To Remove:

UnInstall-Package EntityFramework

To Reinstall:

Install-Package EntityFramework

Or in one command:

Update-Package -reinstall EntityFramework

I hope this can help someone as it took me a while to come to this.

查看更多
爷、活的狠高调
5楼-- · 2019-01-06 14:47

I tried most of the above without luck.

What finally worked was:

  1. Delete the previously dynamically created string entry
  2. Delete my model (keeping all controllers and views)
  3. Recreate the ADO.NET Entity Data Model - using the same name, creating a new connection string entry with the same name as before

Then everything worked again, minus 3 hours of development time.

  1. re-add (from a backup) all of my property attributes to the table/entity classes

Seems to have something to do with the dynamically created connection string and the model. Would appreciate any thoughts on what could have happened.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-06 14:50

In my case NuGet added the following provider to the web.config:

<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact"/>

When I changed the provider to

<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>

It solved the problem (costed me about 5 hours though to figure it out :-( )

查看更多
我只想做你的唯一
7楼-- · 2019-01-06 14:50

I realize this question is old now, but I thought I'd post what solved my issue in case it helps anyone later on. In my case, it was a combination of several things mentioned in other answers. To secure my connection string I had...

  1. Moved the connection string out of the Web.config file
  2. Moved the server name and password to a separate file referenced in the AppSettings portion of Web.config
  3. Used a SqlConnectionStringBuilder to put the elements together and then pass it to my context class constructor

After doing this, I was unable to create any more controllers. To fix the issue, I had to ...

  1. Put the complete connection string back in Web.config AND remove the reference to the external connection string file
  2. Add a parameterless constructor to my context class and give it the name of my connectionString like this: public contextClass() : base("name=connectionStringName") { }

  3. Rebuild the solution, and create the controller again, and it worked!

查看更多
登录 后发表回答