在ASP.NET Web API中采纳OData房地产项目

一.什么是OData OData是贰个开放的数码协议(Open Data Protocol)
在ASP.NET Web API中,
对此CRUD(create, read, update, and
delete)应用比守旧WebAPI扩展了一点都不小的油滑
若是科学行使有关的合计,能够在相同情形下
对三个CRUD应用能够节省很多开发时间,从而压实开发功能

二.怎么搭建

做一个简约的订单查询示例
大家运用Code First形式开创七个实体对象Product(产品),Supplier(供应商)
1.新建1个ASP.NET Empty项目,选取上运用Web API,如下图
房地产项目 1
2.使用NuGet引用OData和EntityFramework程序集
房地产项目 2
三.在Models文件夹中增添Product(产品),Supplier(供应商)四个实体

public class Product    {        public int Id { get; set; }        public string Name { get; set; }        public decimal Price { get; set; }        public string Category { get; set; }        [ForeignKey("Supplier")]        public int? SupplierId { get; set; }        public virtual Supplier Supplier { get; set; }    }public class Supplier    {        public int Id { get; set; }        public string Name { get; set; }        public ICollection<Product> Products { get; set; }    }

4.扩张ProductContext数据库上下文对象,并且在web.config中布署好ConnectionString

public class ProductContext : DbContext    {        public ProductContext()            : base("Demo")        { }        public DbSet<Product> Products { get; set; }        public DbSet<Supplier> Suppliers { get; set; }    }

 

<connectionStrings>        <add name="Demo" connectionString="Data Source=(localdb)\v11.0;         Initial Catalog=Demo; Integrated Security=True; MultipleActiveResultSets=True;         AttachDbFilename=|DataDirectory|Demo.mdf"      providerName="System.Data.SqlClient" />    </connectionStrings>

5.生成数据库
大家运行程序包管控台,运维如下1个指令,
把大家Code First生成的实业生成到数据库
PM> Enable-Migrations
PM> Add-Migration FirstInit
PM> Update-Database
接下来大家能够在服务器能源管理器中来看我们转移的数目库表,如下图
房地产项目 3房地产项目 4
陆.接下来大家在WebApiConfig中登记大家的OData路由

using Demo2.Models;using System.Web.OData.Builder;using System.Web.OData.Extensions;namespace Demo2{    public static class WebApiConfig    {        public static void Register(HttpConfiguration config)        {            ODataModelBuilder builder = new ODataConventionModelBuilder();            builder.EntitySet<Product>("Products");            builder.EntitySet<Supplier>("Suppliers");            config.MapODataServiceRoute("ODataRoute", null, builder.GetEdmModel());        }    }}

七.接下来大家分别为Product和Supplier新建ODataController,
房地产项目 5
房地产项目 6
咱俩在上海体育场所中大家选拔好模型类和数目上下文,
人己一视新上边两步为Supplier实体也扭转对应的ODataController
留意:由于VS20一三的OData模板中OData V叁版本的模板,
引用的名号空间要从V3
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
修改为V4
using System.Web.Http;
using System.Web.OData;

到此,大家的OData示例程序已经搭建完结,我们在浏览器里打开这几个类其他时候
会并发如下

{  "@odata.context":"http://localhost:8914/$metadata","value":[    {      "name":"Products","kind":"EntitySet","url":"Products"    },{      "name":"Suppliers","kind":"EntitySet","url":"Suppliers"    }  ]}

代表曾经搭建成功了

三.怎么使用 由于并未有测试数据,大家开始先在数额库表里为Products和Suppliers增添部分测试数据
房地产项目 7
房地产项目 8
接下去大家看看壹些简约的运用示例
在自动生成的ProductsController和SuppliersController中
早就为大家转移了之类一些Action
房地产项目 9房地产项目 10
从而对于部分扩张,修改,删除,更新本人就不做过多示例,
这个都以和WebAPI未有啥太多差别,
自家第三示例的是查询的施用,不得不说OData已经为我们把询问效能全做完了
以身作则一:列出全数Product
URL:http://localhost:8914/Products

{

"@odata.context":"http://localhost:8914/$metadata#Products","value":[

{

"Id":1,"Name":"Products1","Price":100.00,"Category":"Test","SupplierId":1

},{

"Id":2,"Name":"Products2","Price":200.00,"Category":"Test","SupplierId":1

},{

"Id":3,"Name":"Products3","Price":300.00,"Category":"Test","SupplierId":1

},{

"Id":4,"Name":"Products4","Price":400.00,"Category":"P1","SupplierId":2

},{

"Id":5,"Name":"Products5","Price":500.00,"Category":"P1","SupplierId":2

},{

"Id":6,"Name":"Products6","Price":600.00,"Category":"P1","SupplierId":2

},{

"Id":7,"Name":"Products7","Price":700.00,"Category":"P1","SupplierId":2

},{

"Id":8,"Name":"Products8","Price":800.00,"Category":"Test","SupplierId":3

},{

"Id":9,"Name":"Products9","Price":900.00,"Category":"P1","SupplierId":3

}

]

}

示例二,查询单个Products
URL:http://localhost:8914/Products(1) 其中(1)为Id

{  "@odata.context":"http://localhost:8914/$metadata#Products/$entity","Id":1,"Name":"Products1","Price":100.00,"Category":"Test","SupplierId":1}

示例3,查询Products,只列出Name,Price例
URL:http://localhost:8914/Products?$select=Name,Price

{  "@odata.context":"http://localhost:8914/$metadata#Products(Name,Price)","value":[    {      "Name":"Products1","Price":100.00    },{      "Name":"Products2","Price":200.00    },{      "Name":"Products3","Price":300.00    },{      "Name":"Products4","Price":400.00    },{      "Name":"Products5","Price":500.00    },{      "Name":"Products6","Price":600.00    },{      "Name":"Products7","Price":700.00    },{      "Name":"Products8","Price":800.00    },{      "Name":"Products9","Price":900.00    }  ]}

示范伍:只列出ID为壹的Products,只显示列Name,普赖斯

URL:http://localhost:8914/Products(1)?$select=Name,Price

{  "@odata.context":"http://localhost:8914/$metadata#Products(Name,Price)/$entity","Name":"Products1","Price":100.00}

示例6:列出Products(只有列Name,Price),包括Supplier
URL:http://localhost:8914/Products?$select=Name,Price&$expand=Supplier

{  "@odata.context":"http://localhost:8914/$metadata#Products(Name,Price,Supplier)","value":[    {      "Name":"Products1","Price":100.00,"Supplier":{        "Id":1,"Name":"Supplier1"      }    },{      "Name":"Products2","Price":200.00,"Supplier":{        "Id":1,"Name":"Supplier1"      }    },{      "Name":"Products3","Price":300.00,"Supplier":{        "Id":1,"Name":"Supplier1"      }    },{      "Name":"Products4","Price":400.00,"Supplier":{        "Id":2,"Name":"Supplier2"      }    },{      "Name":"Products5","Price":500.00,"Supplier":{        "Id":2,"Name":"Supplier2"      }    },{      "Name":"Products6","Price":600.00,"Supplier":{        "Id":2,"Name":"Supplier2"      }    },{      "Name":"Products7","Price":700.00,"Supplier":{        "Id":2,"Name":"Supplier2"      }    },{      "Name":"Products8","Price":800.00,"Supplier":{        "Id":3,"Name":"Supplier3"      }    },{      "Name":"Products9","Price":900.00,"Supplier":{        "Id":3,"Name":"Supplier3"      }    }  ]}

示例7:过滤Products,只展现分类为Test的数据
URL:http://localhost:8914/Products?$filter=Category eq ’Test‘

{  "@odata.context":"http://localhost:8914/$metadata#Products","value":[    {      "Id":1,"Name":"Products1","Price":100.00,"Category":"Test","SupplierId":1    },{      "Id":2,"Name":"Products2","Price":200.00,"Category":"Test","SupplierId":1    },{      "Id":3,"Name":"Products3","Price":300.00,"Category":"Test","SupplierId":1    },{      "Id":8,"Name":"Products8","Price":800.00,"Category":"Test","SupplierId":3    }  ]}

示例八:过滤Products,只展示分类为Test的数码,并排序
URL:http://localhost:8914/Products?$filter=Category eq
’Test‘&$orderby=Price desc

{  "@odata.context":"http://localhost:8914/$metadata#Products","value":[    {      "Id":8,"Name":"Products8","Price":800.00,"Category":"Test","SupplierId":3    },{      "Id":3,"Name":"Products3","Price":300.00,"Category":"Test","SupplierId":1    },{      "Id":2,"Name":"Products2","Price":200.00,"Category":"Test","SupplierId":1    },{      "Id":1,"Name":"Products1","Price":100.00,"Category":"Test","SupplierId":1    }  ]}

上边是$filter的别样的运用办法
1.  http://localhost/Products?$filter=Category eq ‘Test’
过滤Category=Test
2.http://localhost/Products?$filter=Price lt 10
过滤Price小于10
3。http://localhost/Products?$filter=Price房地产项目, ge 5 and Price le 15
过滤5<=Price>=15
五,还足以使用数据库函数如:
$filter=substringof(‘zz’,Name)
$filter=year(ReleaseDate) gt 2005

有关排序
$orderby=Price
$orderby=Price desc
$orderby=Category,Price desc

再有一些过滤器如
$skip,$top,$inlinecount等等
从而OData基本上落成我们们想要查询的有关方法
对于CU劲客D程序的查询来说大大进步开发成效

肆: 相关引用能源
https://www.asp.net/web-api
https://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

转发请评释出处:http://giantliu.com

相关文章