I'm trying to run a .NET MVC application on my local computer that I got from GitHub.
When I hit run on Visual Studio, everything complies and a new browser window opens with the error:
CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute'
The compiler section in the same window, shows the following error:
I've checked on google and this seems to be the same error.
This guy was using a dynamic type himself, on the other hand I'm not using any dynamic type, this is being auto generated by .NET's compiler, and I'm using .net 4.5 which should has support for dynamics.
I tried to apply the same solution (Changing all System.Core references to 4.0)
but the thing is that all of them are version 4.0 already.
Do you have any idea how could I resolve this?
I had a similar error on a project I was trying to migrate. Try re-targeting the framework of the project back a version or two, and once you find a target version where you do not get the error, re-target back to the version you originally had trouble with.
I was getting the same error on a website project targeted for 4.6.2 that was referencing some older libraries, re-targeted it back to 4.5.1 and that resolved the error. I immediately re-targeted back to 4.6.2 and did not encounter the error again.
I had this same issue, but none of the answers here solved it, but did point me in the right direction. I think I didn't update the .NET version on my machine to the newer one. After installing it, I rolled back the targeted framework (as was suggested), retargeted the newer framework, and did a few extra things also:
Reinstalled ALL packages by running this in the package manager:
update-package -reinstall
I also compared my project to one that was working for 4.5 and did this:
Opened my project file and removed all "BCL" related elements.
Uninstalled all "BCL" packages in the NuGet package manager for the project (guess it's not needed moving to 4.5+...?)
Removed an old package version of System.Net.Http, which did not get updated for some reason (see web.config - versions should show oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0").
Have to restart VS.
Rebuild solution.
Error is now gone. ;)
One other thing I did to resolve some other errors was to clear the component cache by deleting all files in here:
C:\Users\{USERNAME}\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache\
[rant] Wasted a whole day with all this. Thanks to MS for stealing one more day of my life fixing senseless issues that should never be. Every time I upgrade I always get a wonderful surprise, and a lovely game of fix the red herring. Would it not hurt to detect these conflicts for those upgrading old projects? Perhaps better error traces when things go wrong? I doubt it would be that difficult. Perhaps even something like AsmSpy, but more advanced, built in to VS? [/rant]
Saw this in VS 2019. I excluded and re-included the problematic file (in my case _Layout.cshtml) and that fixed the problem.
I got this error suddenly a couple days after updating to VS2015.3 using 'traditional' MVC app (not new .NET Core / vNext app).
Turns out this error can be a complete red herring.
Make sure:
You haven't got any other errors at all
You've checked the output window for any errors that don't show in the 'Output' window.
Make sure you don't have any conflicting references
In my case I had a typescript error that was causing an error and for some reason triggering this weird 'red herring' dynamic error too.
My fix was to go into the .csproj file and update the typescript tools version to 1.8 (need to unload project and edit by hand). As I said this is unrelated to the error, but I mention the specifics because someone else may end up with this same problem.
I believe that 1.7 didn't accept 'None' for the typescript module system whereas 1.8 does, hence the error.
Adding a reference to System.Dynamic.Runtime did it for me.
I hesitate to write this as an answer, but for me the error appeared in a .cshtml file. I closed the page, rebuilt, and the error vanished. Always best to try the simple things first.
I had the same error, there were just 2 unused tables of my databaseset that I had to delete. I wonder why it worked when I manually change sth in an auto-generated code work but after deleting, it worked.
So be aware that such inconsistencies can also cause this error.
My solution was to correct the version of MVC in web.config under Views folder. E.g. the project referenced MVC assembly version 5.2.2.0 but in the Views/web.config file was version 5.2.4.0 (see image).
Image
Tried a few other answers with no luck, but the overall consensus appears to be that this some sort of issue with cached build artifacts or compiler state or something. Here's what worked for me:
Delete the problem .cshtml file, watch error disappear
Open source control and revert the deletion
This error will come because of the different versions of your Sitecore.Web.Mvc.Dll in solution reference folder(Project name->References->Sitecore.Web.Mvc->right click and check properties) and the version of Sitecore.Web.Mvc.Dll in Web.Config in views Folder.
For solving this error you have to make sure that the version of Sitecore.Web.Mvc.Dll is same in both folders.
Thanx.
For my case, I fixed to run the command line "update-package -reinstall" on the Package Manager Controler.
My problem was in the _Layout.cshtml.
None of the solutions above did the trick for me. I am using VS2017 for this project, upgraded an ran into the same message.
I had commented out a 2 lines in 1 comment #* *#
My solution was to comment out each line separately. What a mystery!
A few hours lost on this.
I've been looking at different options for providing auto generated API Documentation and Swagger seems to be up there.
However when trying this out for the first time I'm getting exceptions on start up.
Running Visual Studio 2013.
Create new Web API project.
Using Package Manager, run:
Install-Package Swagger.Net & Install-Packager Swagger.Net.UI
Making sure we've enable the XML documentation we get this exception:
Method 'GetDocumentation' in type 'Swagger.Net.XmlCommentDocumentationProvider'
from assembly 'Swagger.Net, Version=0.5.1.0, Culture=neutral, PublicKeyToken=null'
does not have an implementation.
Does anyone else know of a step we are missing? We found & followed this:
https://github.com/Swagger-Net/Swagger.Net/wiki
https://github.com/Swagger-Net/Swagger.Net/blob/master/README.md
Swagger doesn't support .NET 4.5 - Had to get the source code out from git and modify it to work with 4.5.
Right click the project and set the target framework in there
I have made a library for use with mvc that generates the documentation for you based on attributes. It may be what your looking for.
MVC3: https://www.nuget.org/packages/SwaggerAPIDocumentation/
MVC4: https://www.nuget.org/packages/SwaggerAPIDocumentation.Mvc4/
MVC5: https://www.nuget.org/packages/SwaggerAPIDocumentation.Mvc5/
MVC5.1: https://www.nuget.org/packages/SwaggerAPIDocumentation.Mvc51/
There is dcoumentation in the repository on github just click the project site link. Hope this helps
Try using Swashbuckle.
Based on Swagger as far as I know and it worked first time for me...
https://github.com/domaindrivendev/Swashbuckle
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.
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.
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.
Possibly the nuget package has a flaw
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.
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.
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:
Open your working folder on your hard drive for your solution.
Delete everything inside the BIN folder
Delete everything inside the OBJ folder
Clean Solution, Rebuild Solution, Add Controller via scaffolding
Voila! (for me)
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:
For each project in solution Project->Properties->Application: Set Target Framework to .Net 4.5 (or whatever you need).
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.
Clean and Rebuild Solution.
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.
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; }
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
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.
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:
Go to your project and update all NuGet packages to latest version.
Build your application till Build success.
Close solution and reopen same.
And try to add file like controller, class, etc.
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.
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.
First, switch to Debug|x86--use Solution (right-click) Configuration Manager for all the projects in your solution. (Debug|Any CPU may also work).
Clean your solution.
Shut down Visual Studio. (cannot get it to work if I skip this).
Open Visual Studio.
Open your solution.
Build your solution.
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.
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.
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
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:
Comment out modelBuilder in your Context class.
Clean Solution, Rebuild Solution.
Add view and Controller via scaffolding
Uncomment modelbuilder.
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.
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.
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.
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).
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>
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.
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.
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.
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".
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.
I'm trying to use my 'context' object in a using statement. It works on one project, but on another, I'm getting the following error.
'...': type used in a using statement must be implicitly convertible
to 'System.IDisposable'
When I'm referring to the 'context' object, I'm referring to the object automatically created when you're working with LINQ to SQL.
The class I'm working within, implements another interface, could that be screwing up this context object?
using (TGDC context = new TGDC())
{
}
the word
using
has the red squigly line under it (error).
You should add a reference to System.Data.Linq. I suspect that's the issue.
Or, if you are using Entity Framework, make sure you have a reference to System.Data.Entity and System.Data.Objects (C#: using / VB.NET: Imports).
Add a reference to the EntityFramework.dll assembly that is referenced by the project that defines your EF context class.
Add a reference to the EntityFramework assembly
This is an older question, but since I found it via a Google search:
I've experienced this while working with a website that worked without issue in Visual Studio 2010.
There's something odd about Visual Studio 2012 and Entity Framework; adding EF to an existing project causes EF 5.0 to be installed (NuGet package), with no option to stick with 4.0.
A comment on this answer to a linked question is what made the realization pop.
Solution seems to be upgrade to the EF version that VS 2012 uses, or use VS 2010.
It says that TGDC does not implement the IDisposable interface.
How does the class declaration for TGDC look? Do you have more compiler errors? Do you have several classes named TGDC in different namespaces?
I get this periodically and for a while I always used to struggle with it because its not immediately evident what's wrong. Seems like all the hoops have been jumped through, but alas, not so. Eventually the solution will stick.
In the project you are writing this code you are referencing the project in which your "TGDC" is defined but you are not referencing System.Data.Linq. You need to reference both these assemblies. This can only really happen when you are using some sort of a layered architecture.