vs2012 + Web API + OData + EF + MYsql development and deployment
First of all, my case, b/s development This piece has been a long time no, what Web API, MVC, OData are just heard, no actual development, because recently to develop a mobile app service, so prepare to use this framework to develop.
The following starts to get to the point (I try to write myself to start writing all the problems encountered in the restore, in order to remind myself later, the article started after I developed the deployment has been written, so some places are missing, please forgive me)
1: First create a new ASP. NET empty Web application with vs2012 (note that I chose the.
2: This time the project should be a blank piece, nothing. First we'll add a few DLLs to the reference "System.web.http,system.web.http.webhost,system.net.http,newtonsoft.json" in the project reference
In addition to Newtonsoft.json, the other three DLLs can be found directly in the reference, Newtonsoft.json need to use the "Package Manager console" to load, command the following "Install-package Newtonsoft.json", Case insensitive
3: Create the model, controller file, in the project location of their own definition, the file code content is as follows:
public class User {public string UserID {get; set;} public string UserName {get; set;} }
public class Usercontroller:apicontroller {public User Get () { return new user () {UserID = "$", User Name = "Admin"}; } public bool Add (user user) { return user! = null; } }
(The above code is I find the demo on the Internet search, not their original, we forgive)
3: Create a routing rule (this should be a knowledge of MVC, just created by vs automatically, and now handwritten by yourself)
Define a class
public class webapiconfig{public static void Register (httpconfiguration config) { config. Routes.maphttproute ( name: "Defaultapi", routetemplate: "Api/{controller}/{id}", defaults:new {id = Routeparameter.optional} ); } }
Then manually add a global file yourself and add the following code
protected void Application_Start (object sender, EventArgs e) { webapiconfig.register ( globalconfiguration.configuration);}
Ok, so far, a Web API program created by VS2012 has been completed.
Launch vs, enter "localhost: port number/api/user" in the browser to access the
(The above code refers to the network information, please forgive me for forgetting the original address.)
All the way up to the top of the code, are very smooth, at least I did not encounter problems in the development process, the following start, there will be a variety of problems.
Start adding support for OData now and use NuGet to add support for OData
, there will be two choices, one for v1-3 and one for the V4 version. Here I chose V1-3, because I heard that the V4 version seems to be not very stable.
(Speaking of the problems I encountered here, when I first created the project, I chose the. NET 4.0 version, if you choose the 4.5 default version, add the OData v4 version will prompt for 4.5 unsupported information, if you add the OData v1-3 version, after the successful addition, run the project,
This time the project will be error, please forgive me Caishuxueqian, did not find out what is the reason to give up. Because I found that using the. NET 4.0 version of the OData V1-3 project is not an error, of course, this is also I have tried a lot of solutions to solve the problem)
After adding the OData component, you need to make some modifications in the project to modify the Usercontroller code for the controller file as follows:
[Queryable]public iqueryable<user> get () { return new user[] {new User () {UserID = "", UserName = "Admin"} }. AsQueryable ();}
OK, it is so simple, now you can browse the data in the browser, I only do a piece of data here, so the view does not see what effect
If you make a few more data, you can access it correctly using the following URL:
localhost: Port/api/user? $top 2
localhost: Port/api/user? $filter =userid EQ ' 000 '
Be aware that OData is case-sensitive.
OK, let's start with EF to add support for MySQL.
First you need to download the installation components: "Mysql-connector-net-6.6.7.msi", this download on the official website is good, but to register, a little trouble.
Then install EF, use the Package Manager console to load, command the following "Install-package EntityFramework",
After installation, restart VS, select "New Item" in the project
Select, fill in the name, click Add
Here are some versions of the choice will be different, I only have these two options, select "Generate From Database", click Next
Here to fill in your MySQL IP, user name, password OK.
(When installing "Mysql-connector-net-6.6.7.msi", if your development machine installed MySQL software, there may be some problems, usually in "programs and features" in the original deleted,
Install the new, or you will choose the data source, there is no "Mysql database" this option)
A "*.edmx" file appears in the project, which is the file created by EF
At this point, create a new controller file, you name it, the code is as follows:
public class Productcontroller:apicontroller { [queryable (allowedorderbyproperties = "id", Allowedqueryoptions = allowedqueryoptions.all)] public iqueryable<map_goodsdiscount> Get () { var db = new Testentities (); Return Db.map_goodsdiscount. AsQueryable (); } }
which
Map_goodsdiscount is the name of your data table in MySQL,
Testentities is the name you fill in with the EF-built MySQL data source
The allowedorderbyproperties in the Queryable property indicates the default sort by the ID field
The Allowedqueryoptions property controls OData query conditions, such as
So far, all the code has been written, and if anything is missing, I'll add it when I find it.
Start the deployment below, deploy the environment for Windows Server R2, have installed 4.0 environment, here will be attention to the deployment of a side-by-side
1: The version of EF in the project, the default is 4.5, the deployment needs to remove the EntityFramework reference, and Add 4.0 version of the DLL, my native 4.0 address is:
C:\Program Files (x86) \microsoft Web TOOLS\PACKAGES\ENTITYFRAMEWORK.5.0.0\LIB\NET40
Modify the configuration file, Web. config to the following format:
<section name= "EntityFramework" type= "System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=5.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirePermission=" false "/>
<section name= "EntityFramework" type= "System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=4.4.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirePermission=" false "/>
2: If you encounter a 404 error while deploying, add the following code in the Web. config
<system.webServer> <validation validateintegratedmodeconfiguration= "false"/> <modules Runallmanagedmodulesforallrequests= "true"/>
All the time, my project from development to deployment has been completed, which has a lot of problems, are online looking for information, coupled with their own a little bit of experience to solve, I hope to other people will have a little help.
Tags: Web API ef OData mysqlvs2012 + Web API + OData + EF + MYsql