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条回答
Rolldiameter
2楼-- · 2019-01-06 14:59

Check relationships between entities or other model design problem. For testing, create new class model without no relationship and use Scaffold to generate controllers and views. Works for me.

查看更多
该账号已被封号
3楼-- · 2019-01-06 14:59

If by any chance you are following "Getting Started with Entity Framework 6 Code First using MVC 5" by Tom Dykstra.

I doubled check everything and my connection string is perfect. What I failed to realize was I copied another set of appSettings which I already have. Look below

  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

My advice is to check every inch of Web.config and I'm sure that is the culprit.

查看更多
贼婆χ
4楼-- · 2019-01-06 15:00

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

base.OnModelCreating(modelBuilder);
查看更多
我想做一个坏孩纸
5楼-- · 2019-01-06 15:00

I was getting the same error when I made some changes to my model .. Only way I was able to resolve was 1) stop/kill the process 2) clean solution and rebuild the solution

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-06 15:02

It seems a problem of connecting setting / inconsistent entry in via Web.config. To fix this issue , follow below steps:

Remove connection related information (staring with <connectionStrings> from Web.config and remove models generated so far.

Now generate the model and it will add fresh connection entry in Web.config file. Once model is generated, build the solution then start doing Scaffolding controlled. it will work.

查看更多
爷的心禁止访问
7楼-- · 2019-01-06 15:04

Using Visual Studio 2015

Upgraded mysql server and in the process the mysql for visual studios was upgraded from 6.9.7 to 6.9.8

In my web config there was still a reference to the old 6.9.7 version

Here is my git diff that solved the issue:

-      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,Version=6.9.7.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"></provider>
+      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6,Version=6.9.8.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"></provider>
查看更多
登录 后发表回答