环境: Visual Studio 2013 + .Net Framework 4.5.2

1.新建项目

2.安装OData,ODP.NET

安装的包:

下面是部分代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace WebAppOdataEF.Models
{
public class AUDIT_TEMPLATE
{
/// <summary>
/// Gets or sets the identifier.
/// 自增ID
/// </summary>
/// <value>The identifier.</value>
public decimal ID { get; set; } /// <summary>
/// Gets or sets the audit template no.
/// 审批模板编码
/// </summary>
/// <value>The audit template no.</value>
public string AUDITTEMPLATENO { get; set; } /// <summary>
/// Gets or sets the name of the audit template.
/// 审批模板名称
/// </summary>
/// <value>The name of the audit template.</value>
public string AUDITTEMPLATENAME { get; set; } /// <summary>
/// Gets or sets the bill type no.
/// 单据类型
/// </summary>
/// <value>The bill type no.</value>
public string BILLTYPENO { get; set; } /// <summary>
/// Gets or sets the remark.
/// 备注
/// </summary>
/// <value>The remark.</value>
public string REMARK { get; set; } /// <summary>
/// Gets or sets the template status.
/// 状态1有效 0无效
/// </summary>
/// <value>The template status.</value>
public decimal TEMPLATESTATUS { get; set; } /// <summary>
/// Gets or sets the company no.
/// 公司编码
/// </summary>
/// <value>The company no.</value>
public string COMPANYNO { get; set; } /// <summary>
/// Gets or sets the create time.
/// 创建时间
/// </summary>
/// <value>The create time.</value>
public DateTime CREATETIME { get; set; } /// <summary>
/// Gets or sets the create user no.
/// 创建人
/// </summary>
/// <value>The create user no.</value>
public string CREATEUSERNO { get; set; } /// <summary>
/// Gets or sets the update time.
/// 更新时间
/// </summary>
/// <value>The update time.</value>
public DateTime UPDATETIME { get; set; } /// <summary>
/// Gets or sets the update user no.
/// 更新人
/// </summary>
/// <value>The update user no.</value>
public string UPDATEUSERNO { get; set; }
}
}

这个是实体代码,类名称和属性名称要和数据库中的表结构表名和字段名称一样,大小写都一样。

EF Context代码:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web; namespace WebAppOdataEF.Models
{
public class TemplatesContext : DbContext
{
static TemplatesContext()
{
Database.SetInitializer<TestsContext>(null);
//Database.SetInitializer(new CreateDatabaseIfNotExists<TestsContext>());
//Database.SetInitializer(new DropCreateDatabaseAlways<TestsContext>());
//Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TestsContext>());
} /// <summary>
/// Initializes a new instance of the <see cref="TemplatesContext"/> class.
/// </summary>
public TemplatesContext()
: base("name=SysBasicOracleDbContext")
{
this.Configuration.LazyLoadingEnabled = false;
} /// <summary>
/// Gets or sets the templates.
/// </summary>
/// <value>The templates.</value>
public DbSet<AUDIT_TEMPLATE> Templates { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("TEST");
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}

  这里是你的用户,

"TEST" 一定要大写
modelBuilder.HasDefaultSchema("TEST");

Controller部分代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.OData;
using WebAppOdataEF.Models; namespace WebAppOdataEF.Controllers
{
public class TemplatesController : ODataController
{
TemplatesContext db = new TemplatesContext();
private bool TestExists(int key)
{
return db.Templates.Any(p => p.ID == key);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
} [EnableQuery]
public IHttpActionResult Get()
{
return Ok(db.Templates);
} [EnableQuery]
public SingleResult<AUDIT_TEMPLATE> Get([FromODataUri] int key)
{
IQueryable<AUDIT_TEMPLATE> result = db.Templates.Where(p => p.ID == key);
return SingleResult.Create(result);
}
}
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAppOdataEF.Models;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Threading.Tasks;
using System.Web.OData; namespace WebAppOdataEF.Controllers
{
public class TestsController : ODataController
{
TestsContext db = new TestsContext();
private bool TestExists(int key)
{
return db.Tests.Any(p => p.ID == key);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
} [EnableQuery]
public IHttpActionResult Get()
{
return Ok(db.Tests);
} [EnableQuery]
public SingleResult<TESTS> Get([FromODataUri] int key)
{
IQueryable<TESTS> result = db.Tests.Where(p => p.ID == key);
return SingleResult.Create(result);
} public async Task<IHttpActionResult> Post(TESTS product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Tests.Add(product);
await db.SaveChangesAsync();
return Created(product);
} public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<TESTS> product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var entity = await db.Tests.FindAsync(key);
if (entity == null)
{
return NotFound();
}
product.Patch(entity);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TestExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(entity);
} public async Task<IHttpActionResult> Put([FromODataUri] int key, TESTS update)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != update.ID)
{
return BadRequest();
}
db.Entry(update).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TestExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(update);
} public async Task<IHttpActionResult> Delete([FromODataUri] int key)
{
var product = await db.Tests.FindAsync(key);
if (product == null)
{
return NotFound();
} db.Tests.Remove(product);
await db.SaveChangesAsync();
return StatusCode(HttpStatusCode.NoContent);
}
}
}

  WebApiConfig代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WebAppOdataEF.Models;
using System.Web.OData.Builder;
using System.Web.OData.Extensions; namespace WebAppOdataEF
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<TESTS>("Tests");
builder.EntitySet<TB_MENU>("TbMenus");
builder.EntitySet<AUDIT_TEMPLATE>("Templates");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel()); // Web API 路由
//config.MapHttpAttributeRoutes(); //config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
}
}
}

  WebApiApplication代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing; namespace WebAppOdataEF
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
}

  WebConfig文件:

<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!--<section name="oracle.manageddataaccess.client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />-->
</configSections>
<appSettings></appSettings>

<!--<connectionStrings>
<add name="TestsContext" providerName="Oracle.ManagedDataAccess.Client"
connectionString="User Id=system;Password=111111;Data Source=XE"/>
<add name="SysBasicOracleDbContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=test;Password=test;Data Source=test" />
</connectionStrings>-->

 <connectionStrings>
<add name="SysBasicOracleDbContext" connectionString="Data Source= (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
    (CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = test)
)
);User ID=test;Password=test;Persist Security Info=True" providerName="Oracle.ManagedDataAccess.Client" /> </connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client" />
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
</system.data>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<!--<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<dataSource alias="XE" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XE))) "/>
<dataSource alias="test" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=test))) " />
</dataSources>
</version>
</oracle.manageddataaccess.client>-->
<entityFramework>
<!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>-->
<defaultConnectionFactory type="Oracle.ManagedDataAccess.EntityFramework.OracleConnectionFactory,
Oracle.ManagedDataAccess.EntityFramework,
Version=6.121.2.0,
Culture=neutral,
PublicKeyToken=89b483f429c47342" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices,Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
</providers>
</entityFramework>
</configuration>

  

运行结果:

http://localhost:54577/Templates

http://localhost:54577/Templates(7)

http://localhost:54577/Templates?$count=true

http://localhost:54577/Templates?$count=true&$select=ID,REMARK

http://localhost:54577/Templates?$count=true&$select=ID,REMARK&$top=2&$skip=1

http://localhost:54577/Templates?$count=true&$select=ID,REMARK&$top=2&$skip=1&$orderby=ID%20desc

http://localhost:54577/Templates?$count=true&$filter=AUDITTEMPLATENO%20eq%20%27t5%27

请参考Odata.org官网:

基础操作:

http://www.odata.org/getting-started/basic-tutorial/#filter

高级操作:

http://www.odata.org/getting-started/advanced-tutorial/

Github地址:

http://odata.github.io/

https://github.com/OData/RESTier

参考文章:

二篇教程(英文版):

1.Using NuGet to Install and Configure Oracle Data Provider for .NET

2.Entity Framework Code First and Code First Migrations for Oracle Database

http://www.cnblogs.com/yjmyzz/p/how-to-use-code-first-in-oracle-with-entity-framework-6.html

http://www.cnblogs.com/shanyou/archive/2010/02/19/1669360.html

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework/

如何使用ASP.NET Web API OData在Oracle中使用Entity Framework 6.x Code-First方式开发 OData V4 Service的更多相关文章

  1. ASP.NET Web API WebHost宿主环境中管道、路由

    ASP.NET Web API WebHost宿主环境中管道.路由 前言 上篇中说到ASP.NET Web API框架在SelfHost环境中管道.路由的一个形态,本篇就来说明一下在WebHost环境 ...

  2. ASP.NET Web API Selfhost宿主环境中管道、路由

    ASP.NET Web API Selfhost宿主环境中管道.路由 前言 前面的几个篇幅对Web API中的路由和管道进行了简单的介绍并没有详细的去说明一些什么,然而ASP.NET Web API这 ...

  3. Global Error Handling in ASP.NET Web API 2(webapi2 中的全局异常处理)

    目前,在Web API中没有简单的方法来记录或处理全局异常(webapi1中).一些未处理的异常可以通过exception filters进行处理,但是有许多情况exception filters无法 ...

  4. 解决ASP.NET Web API Json对象循环参考错误

    前言 一般我们在开法 ASP.NET Web API 时,如果是使用 Entity Framework 技术来操作数据库的话,当两个 Entity 之间包含导览属性(Navigation Proper ...

  5. asp.net web api 的版本升级到 2.2的记录

    asp.net web api 的版本 升级到 2.2的记录 asp.net web api 2.2相比1.0提升了不少 而且其中最重要的就是有了在线文档的自动字段注释的功能 再也不用写详细的字段说明 ...

  6. [水煮 ASP.NET Web API2 方法论](1-5)ASP.NET Web API Scaffolding(模板)

    问题 我们想快速启动一个 ASP.NET Web API 解决方案. 解决方案 APS.NET 模板一开始就支持 ASP.NET Web API.使用模板往我们的项目中添加 Controller,在我 ...

  7. 杂项:ASP.NET Web API

    ylbtech-杂项:ASP.NET Web API ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务. ASP.NET Web A ...

  8. ASP.NET Web API 控制器创建过程(二)

    ASP.NET Web API 控制器创建过程(二) 前言 本来这篇随笔应该是在上周就该写出来发布的,由于身体跟不上节奏感冒发烧有心无力,这种天气感冒发烧生不如死,也真正的体会到了什么叫病来如山倒,病 ...

  9. ASP.NET Web API 管道模型

    ASP.NET Web API 管道模型 前言 ASP.NET Web API是一个独立的框架,也有着自己的一套消息处理管道,不管是在WebHost宿主环境还是在SelfHost宿主环境请求和响应都是 ...

随机推荐

  1. SQL 查询条件放在LEFT OUTER JOIN 的ON语句后与放在WHERE中的区别

    这两种条件放置的位置不同很容易让人造成混淆,以致经常查询出莫名其妙的结果出来,特别是副本的条件与主表不匹配时,下面以A,B表为例简单说下我的理解. 首先要明白的是: 跟在ON 后面的条件是对参与左联接 ...

  2. MySQL 面试基础

    相关:http://blog.csdn.net/u013252072/article/details/52912385          http://blog.csdn.net/zhangliang ...

  3. 【设计模式 - 7】之过滤器模式(Filter)

    1      模式简介 过滤器模式(Filter)也叫标准模式(Criteria),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来. 2      实例 需求 ...

  4. PPT五大插件汇总下载

    收集总结一下PPT制作中常用到的插件/应用,希望能帮到大家. 1.Nordri Tools NT插件是由Nordri公司开发的PPT插件,功能强大,简单易上手,设计偷懒必备神器.我们可以看看它有哪些功 ...

  5. [RxJS] Combination operator: withLatestFrom

    Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...

  6. [RxJS] Combination operators: concat, startWith

    Some Observables may complete, and we may want to append another Observable to the one which just co ...

  7. hadoop编程技巧(4)---总体情况key按类别搜索TotalOrderPartitioner

    Hadoop代码测试版:Hadoop2.4 原理:携带MR该程序随机抽样提取前的输入数据,样本分类,然后,MR该过程的中间Partition此值用于当样品排序分组数据.这使得可以实现全球排名的目的. ...

  8. iOS-Storyboad动态刷新

    iOS-Storyboad动态刷新 什么叫做Storyboard动态刷新 在项目开发中,如果可以在xib(storyboard)中,动态显示运行效果图,那么实在是太爽了.在Xcode 6之后就为我们提 ...

  9. Java基础知识强化之集合框架笔记43:Set集合之TreeSet存储Integer类型的元素并遍历

    1. TreeSet类概述: • 能够对元素按照某种规则进行排序. • 或者根据创建set时提供的Comparator进行排序 • 具体取决于使用的构造方法 2. 代码示例: package cn.i ...

  10. iOS AppIcon + launchImage+iPhone 屏幕分辨率相关知识

    本文主要包含不同iOS 版本的尺寸,分辨率,以及appIcon,launchImage 对不同iOS 版本的适配问题 以下是主要主要的参考资料 https://developer.apple.com/ ...