一、OData介绍

怒放数据左券(Open Data
Protocol,缩写OData)是一种描述怎样创立和会见Restful服务的OASIS标准。

二、OData 在asp.net mvc中的用法

  1、在vs中新建webApi项目

澳门凯旋门游戏网址 1

2、增多测验项目

 public class Product
    {
        public int Id { get; set; }

        public string ProductName
        {
            get; set;
        }
    }

澳门凯旋门游戏网址,凯旋门074网址,3、开启EF自动员搬迁移,增添EF上下文,

namespace ODataTest.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using ODataTest.Models;

    internal sealed class Configuration : DbMigrationsConfiguration<ODataTest.Models.EFContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(ODataTest.Models.EFContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data.

        }
    }
}

 

 public class EFContext : DbContext
    {

        static EFContext()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<EFContext, Configuration>());
        }
        public EFContext() : base("DefaultConnection")
        {
        }

        public DbSet<Product> Products { get; set; }
    }

webAPi OData的使用。4.在数据库增添一些测验数据

webAPi OData的使用。Id ProductName
webAPi OData的使用。21 产品1
22 产品2
23 产品3
24 产品4

webAPi OData的使用。5、在Controllers文件夹增多OData调控器

澳门凯旋门游戏网址 2

6、vs将自动生成基本的CUENVISIOND,

webAPi OData的使用。webAPi OData的使用。将GetProducts方法的EnableQuery天性的AllowedQueryOptions属性设置为:允许全数查询AllowedQueryOptions.All

    public class ProductsController : ODataController
    {
        private EFContext db = new EFContext();

        // GET: odata/Products
        [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
        public IQueryable<Product> GetProducts()
        {
            return db.Products;
        }

        // GET: odata/Products(5)
        [EnableQuery]
        public SingleResult<Product> GetProduct([FromODataUri] int key)
        {
            return SingleResult.Create(db.Products.Where(product => product.Id == key));
        }

        // PUT: odata/Products(5)
        public IHttpActionResult Put([FromODataUri] int key, Delta<Product> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Product product = db.Products.Find(key);
            if (product == null)
            {
                return NotFound();
            }

            patch.Put(product);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(product);
        }

        // POST: odata/Products
        public IHttpActionResult Post(Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Products.Add(product);
            db.SaveChanges();

            return Created(product);
        }

        // PATCH: odata/Products(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public IHttpActionResult Patch([FromODataUri] int key, Delta<Product> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Product product = db.Products.Find(key);
            if (product == null)
            {
                return NotFound();
            }

            patch.Patch(product);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(product);
        }

        // DELETE: odata/Products(5)
        public IHttpActionResult Delete([FromODataUri] int key)
        {
            Product product = db.Products.Find(key);
            if (product == null)
            {
                return NotFound();
            }

            db.Products.Remove(product);
            db.SaveChanges();

            return StatusCode(HttpStatusCode.NoContent);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool ProductExists(int key)
        {
            return db.Products.Count(e => e.Id == key) > 0;
        }
    }

 

7、在WebApiConfig类的Register方法里面注册OData路由

using System.Web.Http;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Extensions;
using ODataTest.Models;

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());

8、运转网址就能够在浏览器就行OData的API测量检验

   常用查询:

  查询全数:

  依据主键举办查询:) 
【22为主键值】

  相等查询:
eq ‘产品1’

      只询问部分字段:

  模糊查询(那一个找了老半天):)
eq true  

  还应该有越来越多的询问,参谋, ,在mvc中动用EF和OData基本可以满意全数的前端查询,有利于快捷支付API查询接口

相关文章