I am relatively new to MVC, so maybe I am missing something. I am using VS 2015 Community Edition, C# 2015 & .NET Framework 4.5.2.
I have an MVC solution with 2 projects: TLM and TLM.Tests. All the .cs files in both projects are happy with the following line of code:
using System.Data;
In fact, one of the .cshtml files has this line:
#using System.Data;
Now I add a 3rd project to the solution, a Class Library called TLM.Helpers, to hold commonly used code. (In ASP.NET web forms, it would have just gone in a .cs file in the App_Code folder.) However, I cannot add a using statement for System.Data to any of the .cs files in this project. In fact, I cannot even add a reference to that DLL in this project, although I see that there is such a reference in the other 2 projects.
That new project does have successful references to System, System.Collections.Generic, and System.Linq.
Other posts for similar issues suggest verifying that the project is being built for the proper Framework version. The Property page for this project does not even offer the choice of Target Framework. It does have dropdowns for "Solution DNX SDK version", which refers to "Version 1.0.0-beta5". I don't know what that refers to.
Any suggestions as to how I can refer to System.Data in this project?
Thanks.
Dan
Related
UPDATED QUESTION
I have a project that I've started in VS 2013 and was setup as the following:
MyMainMvcApp (Containing core Functionality)
MyPlugin (Containing plugable customer stuff)
MyPlugin contains some *.cshtml views as embedded resource and some controllers.
In VS2013 I was able open the Views and i had full Razor Intellisense and no issues when compiling.
IN V2015 the Error List is showing a lot of errors like
Feature 'lambda expression' is not available in C# 2. Please use language version 3 or greater.
The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
etc etc
Of course I have all the Assemblies referenced. And the projects even compile.
I believe that VS 2015 is just missing some config stuff in web.config or similar, so that he can resolve the stuff while having the view open in designer.
I have an app.config in MyPlugin project that has been added from Nuget.
I've put the same config stuff as my web.config in there but that didn't help.
As well I copied the MyMainMvcApp\Views\web.config into MyPlugin\Views\web.config that didn't work as well.
Any idea what the issue might be or ideas how to solve it?
I've started MyPlugin as a Class Library in VS 2013 and that worked well. Afterwards I was not able to make MyPlugin work as desired in VS 2015 designer. I've created a new MyPlugin Project as a MVC Project and copied all my code from the old library to the new.
It's a workaround but that solves my VS 2015 problems.
The fact that it's trying to utilize C# 2, is pretty concerning. Make sure your project is targeting the latest version of .NET you feel comfortable with. Since you're using VS2015, go all the way to 4.6 if you like, or if you want something a little more hardened, stick with 4.5.2. Just right-click your project in the Solution Explorer and choose "Properties". Then on the first "Application" tab, change the "Target framework" drop-down to something appropriate.
I recently inherited some source code developed by someone else who is no longer with the company. However, the solution file was missing and I'm not even sure which version of MVC it was using. I'm experienced with ASP.NET, but not well-versed in MVC so I'm unsure how to go about rebuilding the solution the proper way. Looking for any tips/guidance on how to go about this.
Create a new, blank solution.
Open the solution in Windows Explorer.
Copy the project files into the solution folder.
Within Visual Studio, right-click the solution in Solution Explorer, and then chose the option to Add Existing Project....
Navigate to the solution folder (if necessary), and then select the .csproj (or .vbproj) file. The project will added into the solution.
Now, whether you can compile the solution/project depends on whether the version of MVC is compatible with the tooling installed in your instance of Visual Studio. If the project has NuGet dependencies, they should be restored the first time you build, depending (again) on your version of Visual Studio and tooling.
Edited:
Since you say that even the project file is missing, you can, assuming there are no external dependencies, create a new MVC project and then copy/paste the files into it (or drag & drop the files from Windows Explorer). Assuming there is a Views folder, it will have it's own web.config file. That config file will have bindings for the version of MVC the project was developed with.
MVC4 and newer projects tended to default to using Razor syntax, so the views will be littered with stuff like #Model. If it's using the older WebForms syntax (like <asp:ContentPlaceholder>), it's probably MVC3 or older.
Go to web.config file in the project and look for:
assembly="System.Web.Mvc,Version=3.0.0.0 ..."
In this example is.MVC 3, look for your version
When I want to add System.Web.Mvc by right clicking the project and Add>Reference option, there is only version 3.0.0.0 and 4.0.0.0. However, I created the project as MVC 5. On the other hand, trying to add this reference on Nuget Package, there is no reference with the same name. Instead of this, there is another refernce named So:
1) How can add System.Web.Mvc (version 5 or later) reference to my projects including Class Library?
2) What is the difference between System.Web.Mvc and Microsoft.AspNet.Mvc?
Thanks in advance.
System.Web.Mvc Is one of the core namespaces developed by MS,it contains classes and interfaces that support the ASP.NET Model View Controller (MVC) framework for creating Web applications. This namespace includes classes that represent controllers, controller factories, action results, views, partial view, model binders, and much more. MS Link
Don't get confused with another namespace: Microsoft.Web.Mvcwhich contains classes that support the ASP.NET.MVC framework for creating web applications.
Now back to your first question about Microsoft.AspNet.Mvcthat you can download from Nuget. I think it's the same as System.web.mvc , if you check dll file it creates, you will notice that it has the exact samedll name: System.Web.Mvc.dll. It has the same size, assembly signature, etc.
I experienced this odd issue and had to uninstall/reinstall Microsoft ASP.NET MVC via NuGet Package Manager - for the project in question - before I could add the correct System.Web.Mvc v5.0.
Using Visual Studio 2013, create a new ASP.NET Web Application project using the MVC template. Verify that the Add View dialog functions by right-clicking the "Views\Home" folder and choosing "Add... View".
Now add a new F# Library project to the solution. The Add View dialog should still function correctly.
Now add a project reference from the MVC project to the F# project by right-clicking the References folder, choosing Add Reference, Then browse to Solution...Projects on the left and checking the F# library project that you just added.
At this point, if you right-click the "Views\Home" folder and choose "Add... View" you will get an error in a pop-up alert box instead of the Add View dialog:
There was an error running the selected code generator: 'Object reference not set to an instance of an object.'
Workarounds include, but are hopefully not limited to:
Remove all your references to any F# projects every time you want to add a new view.
Unload any referenced F# projects, but leave the project references in place, every time you want to add a new view.
My questions are:
Are other people seeing this behavior, or is it just me?
Does anyone know of a better work-around than removing references or unloading F# projects?
Does anyone know if Microsoft is already working on fixing this? If not should this be reported to the F# team or the Visual Studio team?
Between this issue and the fact that it is still impossible to use NuGet to add an Entity Framework 6 reference to any F# project, I'm starting to get nervous about Microsoft's level of commitment to the F# language.
Not sure if this will help you but check the version of EntityFramework in your solution. I had two projects within my solution and for whatever reason, the web project had EF 6 and the class library project was EF 6.1. Once I uninstalled EF completely from the solution and reinstalled EF 6.1 on both project, the "Add new view" action started working.
I received the same error message when adding views or controllers in a C# & MVC multi-project solution, using EF 6.1 and VS2013 with Update 3. The problem was because the nuget "packages" folder was marked as read only, because it was checked into source control. I checked the entire folder out, making it editable, and it started working fine, allowing me to scaffold straight from VS.
I also received the error message in a web.api project because I forgot to add the Entity Framework connection string to the web.config (it was using Areas and EF was looking in the Area web.config, not the web.config in the root).
The type 'System.ComponentModel.DataAnnotations.MaxLengthAttribute'
exists in both
[path...]\packages\EntityFramework.4.3.1\lib\net40\EntityFramework.dll
and
'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework
\.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll'
Now, I have read on msdn that its safe to exclude the EntityFramework reference (which was added through the nuget package). However, when I do that, I cant create a DBContext properly as the DbModelBuilder class lives in the EntityFramework dll. Also, some other critical classes are missing when I remove the EntityFramework refference so this is old and irrelevant solution now.
Update (disambiguation): Both System.ComponentModel.DataAnnotations.dll and EntityFramework.dll include System.ComponentModel.DataAnnotations.MaxLengthAttribute. The problem is that each dll also includes other classes that are critical to EF code-first design. For example:
EntityFramework.dll:
- System.Data.Entity.DbModelBuilder
System.ComponentModel.DataAnnotations.dll:
- System.ComponentModel.DataAnnotations.RegularExpressionAttribute
Add this statement to top of your class
using System.ComponentModel.DataAnnotations;
System.ComponentModel.DataAnnotations namespace is distibuted across the EntityFramework.dll and System.ComponontModel.DataAnnotations.dll. So you need to add a reference to both of that in your project to make use of DataAnnotations.
The MaxLenth attribute is present in EntityFramework.dll. So make sure you have that reference to that dll present in your project references section.
EDIT : As of .NET framework 4.5, this namespace is moved to the System.ComponentModel.DataAnnotations.dll. So If you use .NET Framework 4.5 with Entity Framework 4.3.1 or less, You will run in to this conflict. The solution is to switch to Entity framework 1.50 beta 1/ 2 release if you want to stick with .NET 4.5 or downgrade to .NET 4 to use EntityFramework 4.3.1.
From the msdn documentations.
Starting with Entity Framework 5.0 Beta 1, the EntityFramework.dll
does not contain definitions for data annotations. These definitions
were moved to System.ComponentModel.DataAnnotations.dll and are
defined in the System.ComponentModel.DataAnnotations.Schema namespace.
I didn't have the option of upgrading the project to use EF5, or downgrading the build machine to .Net Framework 4.
There is a way to solve this though! It turns out when you install Visual Studio 2012 it adds the following folder (amongst others).
C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
In VS if you open a project targetting 4.0 and look at the properties of your reference to System.ComponentModel.DataAnnotations you'll see that the path is pointing to the above location, rather than the GAC.
This folder contains the original Framework 4.0 assemblies. If they're present on the machine then MSBuild etc., upon building a project targeting 4.0 will reference these rather than the modified ones that 4.5 puts into the GAC.
In our case this meant we could fix the problem by copying that folder from a dev machine with VS installed up to our build server in the same location. (N.b. We only needed to copy this folder, there was no need to install VS on the build server).
More info here: http://marcgravell.blogspot.co.uk/2012/09/iterator-blocks-missing-methods-and-net.html
Hope this helps someone else!