Following the previous MVC & Entity Framework (1)-the development environment, it has not been updated for a long time. Next, take a look at how to separate the controller in MVC into a class library and then reference it in a Web project. Also, by the way, try to split the entity class in models into a separate class library.
1. Controller Class Library Project
① adding a normal class library (. Net Framework4.5)
② enter the following command in the PM console the NuGet command installs the MVC reference-this is the same effect as right-clicking "Add Reference" to the project's "References".
The version number 4.0.20710.0 is specified here, primarily to match the version of the class library that was automatically referenced when the new MVC 4 Web project was created. Can be viewed in the packages.config of the Web project.
4.0. 20710.0
③ Add a Controllers folder to the class library above, and then add the class in this folder, the naming convention is Xxxcontroller. class inherits the controller, and the other notation does not differ.
④ adds a reference to the class Library project in the Web and modifies routeconfig to increase the namespaces parameter to modify the namespace of the specified controller for the registered route.
Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Stock", action ="Index", id =urlparameter.optional}, namespaces:New string[] {"Ivan.ControllerService.Controllers" } ); } }
2. Models Class Library Project
Create a new Class library project, and then add two folders: Models and ViewModels. The former corresponds to the tables in the database, the latter is mainly composed of models, which facilitates the interaction between UI level and data logic layer.
Given the use of the entity Framework, enter the following PM command.
5.0
When you are finished, you can see that the project references more than two DLLs, as follows:
At this point, you can create DataContext as you mentioned in the previous article
MVC & Entity Framework (2)-Controller, models standalone DLL