Enable-Migrations command changed something - c#

Googling this issue, I've tried all the answers to my problem with no change.
I have a small MVC e-commerce app I am building for my company. It is divided into two projects: Domain and WebUI. WebUI is obviously dependent on Domain and my build-order reflects this. I've checked target frameworks for both projects and they match (4.5) and I have EF 5.0 added.
After building the basic product presentation/catalog and cart code I want to implement users so I can build the order/store functions.
Everything is working fine until this point.
I run "Enable-Migrations" so I can write my own membership/user code and use the DB I have setup so future context changes target my DB instead of the defaultOCnnection.
Enable Migrations inserted the Migrations folder and Configuration class. Ran with no errors produced. "Checking if the context targets and exisiting database ... Code First Migrations enabled for project *.WebUI."
When I attempted a Build after running Enable-Migrations, I received a number of errors stating that The type or namespace 'Mvc' does not exist in the namespace 'System.Web' in the Domain project (my product class) and one error 'Could not resolve reference. Could not locate assembly "System.Web.Mvc, Version 4.0.0.0 ...." Check to make sure the assembly exists on the disk ..." And of course an error from WebUI that it could not locate the Domain.dll (because Domain.dll fails to build).
How did running enable-Migrations cause this?
How do I resolve this? The dll is on the disk, right where the properties say it is, Reference included, Local Copy=true ....
I'm really at a loss to figure this one out ....

Strangely, I encountered this error two days ago on one of our machines, the only difference being that we are running EF 6.1.1. I used ReSharper to check that nothing in our domain project was using System.Web.Mvc and I removed the reference. I couldn't figure out how it got there anyway.
We ran enable-migrations maybe a couple of years ago on this code base, so it would seem unlikely that our issue was caused by that.
For your part, there really shouldn't be much of a reason to have a reference to System.Web.Mvc in your domain code, so perhaps you could look at moving whatever code is depending on that DLL into your web project, if it can't be outright deleted, then remove the DLL reference itself?
I know this doesn't explain why it doesn't just work. That might have something to do with assembly redirects in the app.config file, perhaps. Maybe it's redirecting to v5, which isn't there?

Related

WebDAV# SqlExample missing assemblies

I just cloned the WebDAVSharp.SQLExample repo from winterleaf. It uses the WebDAVSharp.Server as base for a WebDAV Server which runs in a (MS)SQL backend, meaning all files/data is stored in DB.
I added the nu-get package of WebDAVSharp.Server to the WebDAVSharp.SQLExample project but still, it shows me missing references. For eg.: in the WebDAVSharp.Data->SqlStoreFileInfo.cs the base class WebDavFileInfoBase is missing. (The're over 200 further errors complaining about missing references)
What makes me curious is the fact I can't find this class in both repos, which basically means it doesnt exist (if I didnt simply overread something).
Does anyone have similiar troubles with WebDAVSharp.SQLExample or does it build without errors (missing references) for you ? If so, I would be very glad about a clue/hint what I am missing.
I recently noted that winterleaf created a pull request in WebDAVSharp.Server which integrated the missing files and assemblies.
So in order to successfully build the WebDAVSharp.SQLExample one need a development build of WebDAVSharp.Server which integrates this pull request.
That's it.

Reference Missing After Nuget Package Update

I want to start off by saying I've been really hesitant to ask this question because it seems like it should be such a simple thing, however, I've scoured the internet and all I can find is the basic solution to something similar to my issue.
I have a nuget package that is v 1.30 and I'm trying to update it to 1.41. I've used the NuGet UI and I've also used the Package Manager Console to do this multiple times with small variations and I continue to get the same result. After I do something like "Update-Package PackageName" my package updates correctly. NuGet deletes the old reference and the old files and the 1.41 version is referenced by all the projects in the solution using that package. When I look at the Code, there are no syntax errors, Visual Studio seems to think the reference is just fine and I can even ctrl+B (re#er) to 'go to declaration.' However, when I build, every place I have 'using PackageName' in code throws an error that says
"The type or namespace name 'Common' does not exist in the namespace 'PackageName' (are you missing an assembly reference?)"
I have tried cleaning my solution. Then building. I've tried resetting through Git, cleaning the solution, updating then building. I've tried updating, cleaning, deleting the reference, building (will get the errors obviously), then adding the reference back in manually and building. Every time I try I get the same error. It's only happening for my Core project, but all my other projects depend on the Core, so they won't build anyway. I'm assuming this issue would still present itself if they were able to build. I've looked at the properties of the reference, it is referencing the v 1.41 file and it is set to Copy Local = True (I read somewhere someone suggested looking at that).
I'm open to suggetions for things to try. I don't have any code changes that I'll lose of I do a Git Reset, so I can try whatever.
Thanks Internet.
After trying several different things, we figured out it was a version mismatch with ASP.NET. Someone had pulled in a newer package that required updating to ASP.NET 4.5 and the solution I was working in was still ASP.NET 4.0. After updating to 4.5 the problem with the references was resolved. But we ran into some new Automapper issues. One thing after another. However, if you're banging your head against your desk in frustration because of this problem, check the framework version of your projects and the package you're updating.

"There was an error running the selected code generator" in VS 2013 scaffolding

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.

Add Controller after recent tools update fails with dependency error

Since installing the recent tools update for MVC3 adding a controller through the "Add Controller" context menu is often failing with the error:
Could not load file or assembly 'Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
Or simimlar (sometimes another dependency, such as "Mono.Addins" etc).
The references it complains about aren't needed by the project directly, but are referenced by other references this project has. The project builds find without the references, but Add Controller fails.
Adding the reference mentioned in the error doesn't fix this either. Even after adding a reference to Newtonsoft.Json (with the correct version and token) the error remains.
I suspect this due to the reflection the Add Controller dialog does to provide a list of model types that can be used, but I can't understand why it still errors even after adding the requested reference.
The "Add View" dialog is slightly better but still fails. The dialog opens, but when you tick "Create a strongly-typed view" the dialog immediately closes.
At the moment my work around is to manually add Controllers, and to add non-strongly types views and edit them by hand afterwards, but this is becoming really frustrating - especially since I can't take advantage of our customer T4 templates for scaffolding views and controllers.
Anyone else experienced this since the update and know how to fix it?
I had the same problem with protobuf.
It means that one of the projects you reference has a dependency on Newtonsoft.Json, my guess is that it's via attributes.
When VS tries to reflect on your assemblies' type to offer you model classes in the dialog (Add Controller / Add View) it can't find Newtonsoft.Json to figure out what it's reflecting, Adding a reference to Newtonsoft.Json to your MVC project should fix that.
Drop all of your dependencies and update their dlls to the version you want to use again. This fixed it for us, was happening to a couple of different libraries, not just one.
I finally solved it. I guess the problem can reside in different places. In my case, I had added models to my domain, but I had forgotten to add contexts for these new models; things like
public DbSet<Region> Regions { get; set; }
I had no error at compile time, and I was not using these models yes in the solution, so there was no error at run time either. It is a bit by chance that I found this. Another thing I have to mention is that I rebooted my machine in the meantime. I don't know if this affected things also, but I rather think the problem was due to the missing contexts.
This has nothing to do with the tools update. I had a similar problem and fixed it by rebuilding my whole dependency tree, starting from the "leaves" (i.e. projects that have no dependecies). There was a similar question already, my answer to it is here: https://stackoverflow.com/a/6627505/342838.
Thanks, this helped me. I had recently added a library called OAuth2.MVC to my MVC4 Web Api project, and then this error started occurring. Oauth2.MVC had a dependency on Newtonsoft.Json version 3.5.0.0, and my project already had version 4.5.0.0. I went back to the OAuth2.MVC project (it's open-source) and upgraded the version of Newtonsoft.Json there to 4.5.0.0, rebuilt it, and then re-imported the reference into my MVC4 Web Api project, and I could then add a controller without the error.

Compile error: 'The type or namespace name 'Runtime' does not exist in the namespace 'MyAppNamespace.System' (are you missing an assembly reference?)'

I am writing a WPF client that consumes services from an ASP web service (VS2010, .net 4.0). It was working just fine until yesterday when I suddenly get the above compiler error. Double-clicking on the error takes me to Reference.cs and highlights just about anything along the lines of:
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=2)]
(The 'Runtime' object is highlighted as not being in the MyAppNameSpace).
I've tried googling this and re-adding the service reference and re-adding the assemby reference but nothing works and I'm completely stuck. Reference.cs is automatically generated and I'm still relatively new to VS2010 so I'm not sure where to go with this.
Hoping someone here can point me in the right direction...?
I had the same problem. After hours of trying to solve it I realised I had a class in my library called system. What a cock.
Your problem is that you had defined a "System" namespace somewhere that the compiler and Intellisense were finding before the real "System". The solution is to remove the offending namespace, then clean and rebuild the project.
I had the same issue with a VB project only I had no old code as it was a new service reference. I did not have any other namespace with System in the name. The solution for me was to make the following series of replacements:
VB
in Reference.vb
Replace System. with nothing
Then replace "Runtime.Serialization" with "System.Runtime.Serialization"
Lastly replace <Global.ComponentModel with <ComponentModel
C#
in Reference.cs (I didn't try this but expect it will work)
Replace System. with nothing
Then replace "Runtime.Serialization" with "System.Runtime.Serialization"
Lastly replace [Global.ComponentModel with [ComponentModel
One other detail that might lead someone to a root cause is that the service wsdl was coming from an old style asp.net web service (.asmx), here: https://api.mindbodyonline.com/0_5/ClassService.asmx?wsdl
Update
I've subsequently found that changing the target framework to 4.5.1 resolved my compilation issue. Also I had the root cause was that I had different framework versions targeted by my web project and the class library containing my service references. Once I changed them all to 4.5.1 everything was happy no changes to Reference.vb was needed.
Well, for the sake of anyone looking for an answer in future - I gave up trying to fix it and copied a backup of the project files over the existing ones and brought the changes since them in manually. That fixed the corrupt referencing issue.
I had a similar problem, but with a different solution.
My project used to be called "JobRunner", but it changed to "NGB.JobRunner". In the project settings however the default namespace and assembly name was still "JobRunner", and the Reference.cs file was being generated with this namespace.
I have no idea why it was causing problems, but when I fixed the default project namespace and assembly name, and re-added the service reference, problem solved!
I had the similar problem. It was the upper case 'T' in time which was causing the issue. I was doing RunTime but the correct was 'Runtime'.
What a time waste due to a typo!!

Categories