I'm a little confused as to what integrated options I have for DI. I see it's pretty straightforward for .net core (for my particular projects), but I don't need to build a cross platform app and don't see the advantage to using core. However, it doesn't look like .net framework applications are still setup with Global.asax and without Startup.cs so does that mean there is no integrated DI option for .net framework 4.7? Do I still need to get a 3rd party solution or is there a way to use the same DI workflow in a .net framework project as is used in a core project?
Dependency Injection is not integrated by default in classic asp.net, you need to add a nuget package to handle DI (only integrated by default in asp.net core).
EDIT: Even though I found out how to do it as explained below, I still ended up going with Autofac because I didn't realize the Microsoft's solution only supports constructor injection, but not property injection.
I found instructions on how to do it here. I know link answers are bad, but I don't have time to do any more than this. If someone else wants to make an answer with full instructions I will mark it.
https://scottdorman.blog/2016/03/17/integrating-asp-net-core-dependency-injection-in-mvc-4/
Also note that if you are not using Owin already, it is not required. You can set it up just the same in Application_Start method of Global.asax. Only change you would need to make is when it references the Startup class in a statement that reflectively gets all the Controller classes, you will need to change that to be the class the code is in (or any other class in your assembly).
I'm building an ASP.NET MVC app with VS 2017, targeting .NET 4.7, and I'm trying to build my models inside of business library being referenced by the web application. So I'm trying to get many of the same classes that come with an MVC project by default into a brand new class library.
Particularly, I'm getting the following error:
This is very different from the previous times this question was posted:
The type or namespace name 'IClientValidatable' could not be found
The type or namespace name 'IClientValidatable' could not be found (are you missing a using directive or an assembly reference?)
Namely, in that they both are solved simply by including the using statement for System.Web.Mvc where IClientValidatable lives.
But I've definitely already done that... Here's the reference manager for the class library with the reference included available for .NET 4.7
As further proof, here's a side by side example of where I can pull in some classes from System.Web.Mvc, but not IClientValidatable
For reference sake, here's the configuration on the class library itself
So did this class move somewhere? Is it available with .NET 4.7?
So the real question seems to be not where did it go, but why the most recent framework is targeting such an old version.
When trying to add a reference to System.Web.MVC while targeting .NET Framework 4.7, the only available option is MVC v2.0.0.0 which is incredibly old. When seeding a new MVC app, it'll come with v5.2.3.0
Here's the Assembly Explorer with both libraries loaded. Common items will be highlighted in each, but IClientValidatable wasn't added until later.
And here's the VS 2017 Reference Manager where you can add references based on your current framework, showing the old MVC library for the new .NET version.
So the question then becomes....
Q: How can I add a reference that is not available in the list of assemblies for my framework?
A: Same way you'd add any other reference - you can browse to a dll or grab it from nuget.
DLL If you already have a web app, there are good odds you can find the dll in your existing packages directory at something like:
\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll
If you prefer Nuget, some minor annoyances are that nuget package names don't exactly sync up with the assemblies they install, it's not easy to browse or search which assemblies come with which packages, and they libraries and frameworks often come bundled with other things you might not need. All that said, you'll be looking for the package called:
Microsoft.AspNet.Mvc on Nuget
Which will install the following libraries:
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.
I created project with name Sorama.CustomAuthentication in that project I installed a package called c# CodeFirst Membership Provider via nuGet package manager.
You can see the reference that has been added to this project here
but it is not letting the CodeFirstMembershipProvider class inherit from MembershipProvider.
Error says MembershipProvider could not be found.
What am I missing?
It looks like you added this to a new Class Library project (i.e. not to the MVC project itself)? If so then most of the references they assume exist for the package (that would be in a template MVC project) are missing from your Class Library references.
That particular NuGet (currently at v1.0.0) frankly isn't a great package. It seems to be missing dependencies like EntityFramework and some .NET framework references, so once you get past this one you may have others to add as well, which will probably be:
From Nuget:
EntityFramework.dll, v5.0.0.0 (Install-Package EntityFramework -Version 5.0.0)
From the GAC (i.e. use the Add Reference context menu for the project, and in the dialog search in the Assemblies\Framework list for these names)
System.Web.dll, v4.0.0.0
System.Web.ApplicationServices.dll, v4.0.0.0
System.Configuration.dll, v4.0.0.0
In future you can work this out by asking Visual Studio to build the project, and looking at the build error messages, which will look like:
The type name 'MembershipProvider' could not be found. This type has been forwarded to assembly 'System.Web.ApplicationServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Consider adding a reference to that assembly.
The bit I have highlighted in bold tells you that it thinks the best match for your reference that you need is to System.Web.ApplicationServices, particularly v 4.0.0.0, and gives you the public key token as well.
Be sure to have a reference added to System.Web.ApplicationServices.dll
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am VERY new to ASP.NET MVC (3) and am having a hard time resolving a build error in Visual Studio:
The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
namespace MyProjectName.Models
{
public class MachineModel
{
// name
[Required]
[Display(Name = "Nom de la machine")]
public string Name { get; set; }
// IP
[Required]
[RegularExpression(#"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)",
ErrorMessage = "Donnez une adresse IPv4 valide.")]
[Display(Name = "Adresse IP de la machine")]
public string IP { get; set; }
}
public class MachineDbContext : DbContext
{
public DbSet<MachineModel> Machines{ get; set; }
}
}
The two errors I am getting are:
The type or namespace name
'DbContext' could not be found (are
you missing a using directive or an
assembly reference?)
The type or namespace name 'DbSet'
could not be found (are you
missing a using directive or an
assembly reference?)
What am I missing?
I had the same issue. Turns out, you need the EntityFramework.dll reference (and not System.Data.Entity).
I just pulled it from the MvcMusicStore application which you can download from: http://mvcmusicstore.codeplex.com/
It's also a useful example of how to use entity framework code-first with MVC.
You need to reference the System.Data.Entity assembly in your project, or
install the EntityFramework NuGet package, which will setup everything for you.
Just a quick note. It is DbContext, not DBContext. i.e. with a lowercase 'B'. I discovered this because I had the same problem while intelesense was not working until I tried typing the full name space System.Data.Entity... and name and finally it suggested the lowercase 'b' option:-
System.Data.Entity.DbContext
I had the same problem using VS2010.
I know this isn't really an answer. I just thought it might help someone.
I resolved it by using the fully qualified name for DBContext.
Instead of
public class MachineDbContext : DbContext
I used
public class MachineDbContext : System.Data.Entity.DbContext
and rebuilt the project. Suddenly VS was happy, and I was even able to remove the fully qualified name, and just use DBContext.
I had the same issue... Installing the EF from Package Manager Console worked for me
the command was: Install-Package EntityFramework
If your compiler doesn't recognize
System.Data.Entity.Infrastructure
DbContext
DbSet
et alii,
make sure
you have Entity Framework 4.1 installed on your machine;
in your .csproj file, you have the following reference
<Reference Include="EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" />
within the element
<ItemGroup>
...
</ItemGroup>
that houses references to other assemblies.
I had the same problem..I have VS2010 express..
(Note: If you see this problem try checking references to EntityFramework.dll ..
May be it is missing.)
The following resolved it for me.
I installed latest MVC 3 Tools Update
then I installed EntityFramework 4.1
or using
NUGet ie. from with Visual Studio 2010 Express
(Tools->Library Package Manager -> Add library Package reference -> Select Online -> EntityFramework)
Strangely that didnt work..So i had to manually add a reference to "EntityFramework.dll"
try doing a search for the dll ..may be here
"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\"
If you already have it..just add a '.net' reference.
Note: If you use NuGet ,it creates a folder "packages" along side your Solution directory.
You will find the "EntityFramework.4.1.10331.0" folder inside it.Within "Libs" folder you will find
"EntityFramework.dll" .
Add reference to it using Browse tab and select the above dll.
I am using Visual Studio 2010 express and adding a reference to C:\Program Files\Microsoft ADO.NET Entity Framework 4.1\Binaries\EntityFramework.dll solved the problem.
This helps really handy:
Select the ProjectNAme project in Solution Explorer.
From the Tools Menu, choose Library Package Manager which has a
sub-menu.
From the sub-menu choose Package Manager Console.
At the console’s PM prompt type install-package EntityFramework then
hit enter.
For step-by-step instructions, see this new MVC / EF tutorial series:
http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
The tutorial assumes you have installed the latest MVC 3 Tools Update and provides a link in case you haven't.
Your project unable to resolve EntityFramework classes until you not added it in your project.
For adding EntityFramework support you have to follow this steps:
Tools->Nuget Package Manager ->Manage Nuget package for solution
browse EntityFramework
It shows latest stable EntityFramework version.
currently 6.1.3 is latest version
Install it for the selected project.
Download http://www.dll-found.com/download/e/EntityFramework.dll
Paste it in (for x86)
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\
Then Right click on project -> add reference -> select EntityFramework
Bingo......
Visual Studio Express SP1
Right click in Solution Explorer > References > Add Library Package Reference > EntityFramework
Use CTP5 instead it is newer version with some changes in API. You have to add reference to EntityFramework.dll which is installed in CTP directory - default is: c:\Program Files\Microsoft ADO.NET Entity Framework Feature CTP5\Binaries\EntityFramework.dll for 32bit system. On 64bit system it will be placed in Program Files (x86).
This happened to me when I branched code. To fix it I right-clicked my project in Visual Studio, chose manage Nu-get packages, uninstalled EntityFramework, then re-installed it. Problem solved
I just had this issue and none of the other solutions worked for me. I'm using Visual Studio 2010 and I have two projects in my solution: UIProject and DataAccessProject. The UIProject has a reference to the DataAccessProject.
My UIProject was targeting the .Net Framework 4 but my DataAccessProject was targeting the .Net Framework 3.5 for some reason.
I changed both projects to target .Net Framework 4
Uninstalled then re-installed the Entity Framework NuGet Package.
Problems solved. Now I have references to both EntityFramework and System.Data.Entity
i had the same problem.I had to rewrite "DBContext" after adding reference to entityframework and it resolved the problem.
its means that Visual studio was not able to recognize the class automatically and waited for it to be re-typed.
so its best to add reference first and then implement code.
VS 2013 - ultimate used
I had to first uninstall EntityFramework(Uninstall-package EntityFramework (not case sensitive apparently)), then install it again(Install-package EntityFramework), via the PM Console
I had this problem, read the above answer and download the entityframework.ddl but found that it is alreadt referenced. So I added the namespace and problem was solved
using System.Data.Entity;
I am using Visual Studio 2010, SP1 installed
having referenced entityframework.dll both system.data.entity worked.
I had the same error but the problem was just an accidental problem with my model.
I accidentaly put...
public class MyModelDBContext : Context
{
public DBSet<MyModel> MyModels { get; set; }
}
...inside of the model class.
As alternative way you can go HERE - instruction how to install any required dll.
Or you can download NuGet and manage it from VS
Right click your reference and go to manage NuGet packages, then choose online all, then NuGet package source in the search textbox type Entity Framework and install it.
1) Uninstalling Entity Framework from All projects
2) Restart Visual Studio
3) Reinstalling to all required projects
and it started working
There might be a case where you reference everything you need to and you can even Go to Definition by pressing F12 on DbContext class which takes you to System.Data.Entity namespace but you still get this nasty compiler warning. Make sure that the Target Framework of your project and that of the Entity Framework version you are using match. Go to Project->Properties->Application Tab. Change the Target Framework(can't exactly say to which one, trial and error will help.). Just my two cents.
I also get irritated from this issue many times and finally find the solution.Go to edmx file->Update Model from Database->Refresh->Finish.
Clean Solution->Rebuild Solution
you can try on package manager console
PM> EntityFrameWork\enable-migrations
Like the others have suggested:
Add the correct references and directives. But it still doesn't work? Maybe you have the same problem I did:
Have a look below and see if you can tell me what is wrong:
public class PanelLengthContext : DBContext { } ??!
Make sure the class name is not misspelt - (case sensitivity)!
DbContext is the correct spelling.
this is how it should look:
check the spelling. don't waste 20 min of your life like i did. public class PanelLengthContext : DbContext {}
HTH
I have the same issue as you, I'm unable to implement it in the Controller class while it works when I put it in the model class. Add these codes on the top of your controller class
using TimeSheetManagementSystem.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;