41

I'm creating a new view off of a model.
The error message I am getting is

Error
There was an error running the selected code generator:
'Access to the path
'C:\Users\XXXXXXX\AppData\Local\Temp\SOMEGUID\EntityFramework.dll' is denied'.

I am running VS 2013 as administrator.

I looked at Is MvcScaffolding compatible with VS 2013 RC by command line? but this didn't seem to resolve the issue.

VS2013 C#5 MVC5 Brand new project started in VS 2013.

Community
  • 1
  • 1
Brian Webb
  • 1,146
  • 1
  • 14
  • 29
  • 1
    Please note that this has been answered several times below. Including what I discovered as the solution for my particular issue, which I tagged as answer. But thx for the continual feedback, since this looks like it helps other developers too. – Brian Webb Apr 03 '15 at 01:51
  • BTW, I got this error in VS2017 because my class was using **expression-bodied properties** (i.e. `public string FirstName => "Name";` vs `public string FirstName { get { return "Name"; }; }`) – KyleMit Nov 01 '17 at 14:47
  • This issue was fixed in VS 2019 16.0.3 (https://learn.microsoft.com/en-gb/visualstudio/releases/2019/release-notes#16.0.3) – Mariusz May 01 '19 at 09:12
  • Does this answer your question? [Error : Package restore failed](https://stackoverflow.com/questions/44509694/error-package-restore-failed) – Sayyed Dawood Apr 27 '21 at 22:47

25 Answers25

25

VS2013 Error: There was an error running the selected code generator: ' A configuration for type 'SolutionName.Model.SalesOrder' has already been added ...'

I had this problem while working through a Pluralsight Course "Parent-Child Data with EF, MVC, Knockout, Ajax, and Validation". I was trying to add a New Scaffolded Item using the template MVC 5 Controller with views, using Entity Framework.

The Data Context class I was using including an override of the OnModelCreating method. The override was required to add some explicit database column configurations where the EF defaults were not adequate. This override was simple, worked and no bugs, but (as noted above) it did interfere with the Controller scaffolding code generation.

Solution that worked for me:

1 - I removed (commented out) my OnModelCreating override and the scaffolding template completed with no error messages - my controller code was generated as expected.

2 - However, trying to build the project choked because 'The model had changed'. Since my controller code was was now properly generated, I restored (un-commented) the OnModelCreating override and the project built and ran successfully.

Liam
  • 27,717
  • 28
  • 128
  • 190
Bill B
  • 351
  • 3
  • 4
  • If possible, you may want to switch to EF 6. Apparently there were some changes to the OnModelCreating process that makes coding against it more reliable. I noticed that on my newer model libs that I don't have problems with the OnModelCreating process. – Brian Webb Aug 18 '14 at 19:07
  • 5
    I am following the same course as above, but using EF 6 (and VS 2013 as well). And I get the same error. In the above step 1, remember to rebuild the solution after commenting out the OnModelCreating or it wont create a new scaffolded item. – olf Sep 13 '14 at 18:36
  • This specific answer and especially olf's comment above unblocked me on the same tutorial. Thanks for adding the comments here. – etechpartner Sep 04 '15 at 21:58
17

Problem was with a corrupted web.config and package directory.

I created the new project, and copied my code files over to the new working project, I later went back and ran diffs on the config files and a folder diff on the project itself.

The problem was that the updates had highly junked up my config file with lots of update artifacts that I ended up clearing out.

The second problem was that the old project also kept hanging onto older DLLs that were supposed to be wiped with the application of the Nuget package. So I wiped the obj and bin folders, then the package folder. After that was done, I was able to get the older project repaired and building cleanly.

I have not looked into why the config file or the package folder was so borked, but I'm assuming it is one of two things.

  1. Possibly the nuget package has a flaw
  2. The TFS source control blocked nuget from properly updating the various dependencies.

Since then, before applying any updates, I check out everything. However, since I have not updated EF in a while, I no evidence that this has resolved my EF or scaffolding issue.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Brian Webb
  • 1,146
  • 1
  • 14
  • 29
  • Make sure that you have everything checked out, and if your solution is made up of multiple projects, then each project will have to have it's dependencies updated too. Then try again. The vast majority of the people who have tried this works, so the odds are, you have a file, or something that is still locked, and preventing this correction from working. – Brian Webb Nov 05 '15 at 21:26
  • 1
    Deleting the packages also worked for me. None of the other solutions listed here or in other answers to the same question worked. – jimboweb Dec 07 '16 at 21:02
13

I was able to resolve this issue and have a little better understanding of what was going on. The best part is that I am able to recreate the issue and fix it to be sure of my explanation here. The resolution was to install exactly same version of Entity Framework for both Data Access Layer project and the Web Project.

My data access layer had Entity Framework v6.0.2 installed using NuGet, the web project did not have Entity Framework installed. When trying to create a Web API Controller with Entity Framework template Entity Framework gets installed automatically but its one of the older version 6.0.0. I was surprised to see two version of Entity Framework installed, newer on my Data Layer project and older on my Web Project. Once, I removed the older version and installed the newer version on Web Project the problem went away.

isingh
  • 141
  • 1
  • 5
  • Well, I have only one project in the solution. – Stack0verflow Nov 05 '15 at 20:26
  • I checked the versions and the were exactly the same, anyway, in the web api project i removed reference to EntityFramework and EntityFramework.SqlServer and re-added them then everything worked fine – Saeedouv Feb 14 '16 at 12:45
13

I tried every answer on every website I found, and nothing worked... until this. Posting late in case anyone like me comes along and has the same frustrating experience as I have.

My issue was similar to many here, generic error message when trying to use scaffolding to try and add a new controller (ef6, webapi). I initially was able to use scaffolding for about 15 controllers, after that it just stopped working one day.

Final Solution:

  1. Open your working folder on your hard drive for your solution.
  2. Delete everything inside the BIN folder
  3. Delete everything inside the OBJ folder
  4. Clean Solution, Rebuild Solution, Add Controller via scaffolding

Voila! (for me)

erikrunia
  • 2,371
  • 1
  • 15
  • 22
12

I checked all my projects and each had the same version of Entity Framework. In my case, the problem was that one of my projects was targeting .Net 4.0 while the rest were .Net 4.5.

Solution:

  1. For each project in solution Project->Properties->Application: Set Target Framework to .Net 4.5 (or whatever you need).
  2. Tools->Manage NuGet Package for Solution. Find Installed “Entity Framework”. And click Manage. Uncheck all projects (note the projects that require EF). Now, Re-Manage EF and check that projects that you need.
  3. Clean and Rebuild Solution.
Jess
  • 23,901
  • 21
  • 124
  • 145
RitchieD
  • 1,831
  • 22
  • 21
4

This is typically caused by an invalid Web.config file. I had the same problem and it turned out I inadvertently changed the HTML comment block <!-- --> to a server side comment block @* *@ (through a Replace All action).

And in case you are developing a WinForms application, try to look to App.config.

Moslem Ben Dhaou
  • 6,897
  • 8
  • 62
  • 93
  • Not in my case. If you look at what was tagged as answer, it was a mismatch of dependencies. It was solved by purging the nuget cache, and downloading only the latest of each library for the assemblies. Though I can see where your case could be a damaged web.config file. – Brian Webb May 26 '14 at 14:50
  • this was the answer for me. My configSections wasnt the first child node of configuration node (I squeezed connectionStrings node in there). Of course VS2015 didn't provide any help with the ambiguous error message. I didn't figure it out until I undid web.config changes. – Tom McDonald Dec 07 '16 at 17:40
4

I have the exact same problem. First encountered this while following along the Pluralsight Course "Parent-Child Data with EF, MVC, Knockout, Ajax, and Validation".

I am using MVC 5, EF 6.1.1 and framework 4.5.2.

Even after updating my VS2013 to update 4, this error still persisted.

Was able to circumvent this annoying problem by changing the DbSet to IDbSet inside the DbContext class. Answer was originally from here.

//From
public DbSet SalesOrders { get; set; }

//To
public IDbSet SalesOrders { get; set; }
Community
  • 1
  • 1
scyu
  • 51
  • 4
3

What worked for me to resolve this: Close Solution, And open the project by clicking project file and not the solution file, add your controller, and bobs your uncle

Gerrie Pretorius
  • 3,381
  • 2
  • 31
  • 34
3

None of the above helped for me.

I found that the cause of my problem was overriding OnModelCreating in my context class that the scaffold item was dependent on. By commenting out this method, then the scaffolding works.

I do wish Microsoft would release less buggy code.

Jim Taliadoros
  • 495
  • 4
  • 13
  • That isn't a bug. OnModelCreating describes your model to the EF framework, so it knows how to work with it. Even if you don't use that feature, it is still referenced under the hood. This is why DbModelBuilder is passed as part of the function call. You then can use modelBuilder to extend the various entities that you are working with. These descriptions cover things like cascading updates and optional references as defined in your schema. – Brian Webb Aug 12 '14 at 03:04
  • What you described wasn't a scaffolding issue, but a post scaffold issue where you injected your own bug into the code by overriding the OnModelCreating. Though, I hope this gives other people a heads up on that risk. So thanks for posting. – Brian Webb Aug 12 '14 at 03:06
3

There was an error running the selected code generator:
'Failed to upgrade dependency information for the project. Please restore the project and try again.'

Steps:

  1. Go to your project and update all NuGet packages to latest version.
  2. Build your application till Build success.
  3. Close solution and reopen same.
  4. And try to add file like controller, class, etc.

error picture

TylerH
  • 20,799
  • 66
  • 75
  • 101
2

I have seen this error with a new MVC5 project when referencing a model from a different project. Checking the path, EntityFramework.dll did exist. It was read-only though. Process monitor showed that there was an error attempting to delete the file. Setting the EntityFramework.dll in my packages folder (copy stored in source control) to writeable got around this error but brought up another one saying that it couldn't load the EntityFramework assembly because it didn't match the one referenced. My model class was defined in a different project that was using an older version of the entity framework. The MVC5 project was referencing EF 6 while the model was from a project references EF 4.4. Upgrading to EF 6 in the model's project fixed it for me.

Lindsey
  • 451
  • 4
  • 3
2

For us it has something to do with build configurations, where we have a Debug|x64 build configuration that we had recently switched to using, which in retrospect seemed to be when the scaffolding stopped working.

(I suspect that there are at least 10 different things that can cause this, as evidenced by the various answers on SO that some people find to work for them--but which don't work for others, so I'm not suggesting my solution will work for everyone).

What worked for us (using VS 2013 Express for Web on 64 bit Windows 7):

It (scaffolding) was NOT working in Debug|x64 Build configuration. But doing the following (and it seems like every step is necessary--couldn't figure out how to do it in a more streamlined way) seems to work for us.

  1. First, switch to Debug|x86--use Solution (right-click) Configuration Manager for all the projects in your solution. (Debug|Any CPU may also work).
  2. Clean your solution.
  3. Shut down Visual Studio. (cannot get it to work if I skip this).
  4. Open Visual Studio.
  5. Open your solution.
  6. Build your solution.
  7. Now try adding scaffolding items; for us, it worked at this point, we no longer got the error message saying something about "There was an error running the selected code generator".

If you need to switch back to a scaffolding-non-working build configuration, you can do so, after you've scaffolded everything you need to for the moment. We switched back to our Debug|x64 after scaffolding what we needed to.

DWright
  • 9,258
  • 4
  • 36
  • 53
1

I had this problem when trying to add an Api Controller to my MVC ASP.NET web app for a completely different reason than the other answers given. I had accidentally included a StringLength attribute with an IndexAttribute declaration for an integer property due to a copy and paste operation:

[Index]
[IndexAttribute("NumTrainingPasses", 0), StringLength(50)]
public int NumTrainingPasses { get; set; }

Once I got rid of the IndexAttribute declaration I was able to add an Api Controller for the Model that contained the offending property (NumTrainingPasses).

To help the search engines, here is the full error message I got before I fixed the problem:

There was an error running the selected code generator:

Unable to retrieve metadata for 'Owner.Models.MainRecord'. The property
'NumTrainingPasses' is not a String or Byte array. Length can only be configured for String or Byte array properties.

Robert Oschler
  • 14,153
  • 18
  • 94
  • 227
1

My issue was similar to many experience here, generic error message when trying to add a new view or use scaffolding to add a new controller.

I found out that MVC 5 and EF 6 modelbuilder are not good friends:

My Solution:

  1. Comment out modelBuilder in your Context class.
  2. Clean Solution, Rebuild Solution.
  3. Add view and Controller via scaffolding
  4. Uncomment modelbuilder.
TylerH
  • 20,799
  • 66
  • 75
  • 101
freddy
  • 21
  • 4
1

This is usually related to a format of your Web.config

Rebuild solution and lookup under Errors, tab Messages. If you have any format problems with a web.config you will see it there. Fix it and try again.

Example: I had connectionstring instead of connectionString

bummi
  • 27,123
  • 14
  • 62
  • 101
Marko
  • 1,874
  • 1
  • 21
  • 36
0

In case it helps anyone, I renamed the namespace that the model resided in, then rebuilt the project, then renamed it back again, and rebuilt, and then it worked.

Adam Marshall
  • 3,010
  • 9
  • 42
  • 80
0

I often run into this error working with MVC5 and EF when I create the models and context in a separate project (My data access layer) and I forget to add the context connection string to the MVC project's Web.Config.

John S
  • 7,909
  • 21
  • 77
  • 145
0

I am also having this issue with MSVS2013 Update 4 and EF 6.0 The message I was getting was:

    there was an error running the selected code generator.
A configuration for type XXXX has already been added ...[]

I have a model with around 10 classes. I scaffolded elements at the beginning of the project with no problems.

After some days adding functionality, I tried to scaffold another class from the model, but an error was keeping me from doing it.

I have tried to update MSVS from update 2 to update 4, comment out my OnModelCreating method and other ideas proposed with no luck.

As a temporary way to continue with the project, I created a different asp.net project, pasted there my model classes (I am using fluent api, so there is little annotation on them) and successfully created my controller and views.

After that, I pasted back the created classes to the original project and corrected some mistakes (mainly dbset names).

It seems to be working, although I suppose that I will still find mistakes related to relationships between classes (due to the lack of fluent configuration when created).

I hope this helps to other users.

jmcm
  • 23
  • 5
0

This happened to me when I attempted to create a new scaffold outside of the top level folder for a given Area.

  • MyArea
    | - File.cs (tried to create a new scaffold here. Failure.)

I simply re-selected my area and the problem went away:

  • AyArea (Add => new scaffold item)

Note that after scaffold generation you are taken to a place where you will not be able to create a new scaffold without re-selecting the area first (in VS 2013 at least).

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
0
  • vs2013 update 4
  • ef 5.0.0
  • ibm db2connector 10.5 fp 5

change the web.config file as such: removed the provider/s from ef tag:

<entityFramework>
</entityFramework>

added connection string tags under config sections:

</configSections>
<connectionStrings>
<add name=".." connectionString="..." providerName="System.Data.EntityClient" />
</connectionStrings>
gummylick
  • 1
  • 1
0

I had the same problem when in my MVC app EF reference property (in Properties window) "Specific version" was marked as False and in my other project (containing DBContext and models) which was refrenced from MVC app that EF reference property was marked as True. When I marked it as False everything was fine.

Iwona Kubowicz
  • 364
  • 3
  • 15
0

In my case, I was trying to scaffold Identity elements and none of the above worked. The solution was simply to open Visual Studio with Administrator privileges.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Hugo Nava Kopp
  • 2,906
  • 2
  • 23
  • 41
-1

Rebuild the solution works for me. before rebuild, I find references number of my 'ApplicationDbContext' is zero, that is impossible, so rebuild solution, everything is OK now.

simon9k
  • 5
  • 1
  • Just to add rebuilding for some reason twice resolved this for me. Looks like the error is very genric as so many different answer seem to resolve it what apprenelty is the error. – Phil3992 Apr 07 '17 at 13:12
-2

I had this issue in VS 2017. I had Platform target (in project properties>Build>General) set to "x64". Scaffolding started working after changing it to "Any CPU".

billw
  • 120
  • 1
  • 9
-2

It may be due to differences in the versions of nuget packages. See if you have this by going to dependencies->nuget packages folder in your solution. Try installing all of them of a single version and restart the visual studio after cleaning the componentmodelcache folder as mentioned above. This should the get the work done for you.

shashi kumar
  • 47
  • 1
  • 1
  • 4