Tool:Visual Studio 2013 Ultimate Update 2
OS:Windows 8.1
.NET Framework : 4.5.x
Entity Framework : 6.1.x版以上
ASP.NET MVC 5
step-by-step : 在MVC5 專案之中使用WCF Data Service
- New Project
- Empty MVC
- 安裝Microsoft.OData.EntityFrameworkProvider (Entity Framework Provider for OData)
- 加入ADO.NET Entity Data Model (Database First方式)
- 使用Pubs資料庫
- 按Finish
- 加入WCF Data Service
- 修改程式,改繼承 EntityFrameworkDataService<T>
public class WcfDataService1 : EntityFrameworkDataService<PubsEntities> {
public static void InitializeService( DataServiceConfiguration config ) {
config.SetEntitySetAccessRule( "*" , EntitySetRights.All );
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
public static void InitializeService( DataServiceConfiguration config ) {
config.SetEntitySetAccessRule( "*" , EntitySetRights.All );
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
- 開放路由
public class RouteConfig {
public static void RegisterRoutes( RouteCollection routes ) {
routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
routes.IgnoreRoute( "WcfDataService1.svc/{*pathInfo}" );
routes.MapRoute(
name: "Default" ,
url: "{controller}/{action}/{id}" ,
defaults: new { controller = "Home" , action = "Index" , id = UrlParameter.Optional }
);
}
}
- 測試
- 看到Aton feed:
- 查詢第一筆資料,ID為6380
- 後續便可以使用OData URI Convention進一步查詢,如找尋 Jobs資料表,job_id大於5的資料
沒有留言:
張貼留言