精進不休 .NET 4.5 (11) - ADO.NET Entity Framework 5.0 新特性, WCF Data Services 5.0 新特性(OData V3)
作者:webabcd
介紹
精進不休 .NET 4.5
- ADO.NET Entity Framework 5.0 新特性
- WCF Data Services 5.0 新特性(OData V3)
示例
一、ADO.NET Entity Framework 5.0 新特性
1、演示 ef5 對枚舉類型的支持
EnumSupport.aspx.cs
/* * 演示 EF5.0 對枚舉的支持 * * 注:相關枚舉設計請打開模型設計器查看,自動生成的枚舉類型的代碼在 NorthwindModel.edmx/NorthwindModel.tt/ReorderLevel.cs */ using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace EF50 { public partial class EnumSupport : System.Web.UI.Page { // 通過模型設計器設計了如下枚舉 /* public enum ReorderLevel : short { level1 = 0, level2 = 5, level3 = 10, level4 = 15, level5 = 20, level6 = 25, level7 = 30 } */ // 注:各種相關信息可以打開模型設計器后,在模型瀏覽器視圖中查看 protected void Page_Load(object sender, EventArgs e) { using (var db = new NorthwindEntities()) { var p = db.Products.First(); lblMsg.Text = p.ReorderLevel.ToString(); } } } }
2、演示如何通過存儲過程返回多個結果
GetCategoriesAndProducts.sql
/* * 用於演示如何通過存儲過程返回多個結果 */ CREATE PROCEDURE [dbo].[GetCategoriesAndProducts] AS SELECT * FROM dbo.Categories SELECT * FROM dbo.Products
StoredProcedureWithMultipleResults.aspx.cs
/* * 演示如何通過存儲過程返回多個結果 */ using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using System.Data.Objects; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace EF50 { public partial class StoredProcedureWithMultipleResults : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { using (var db = new NorthwindEntities()) { // 指定存儲過程 var cmd = db.Database.Connection.CreateCommand(); cmd.CommandText = "[dbo].[GetCategoriesAndProducts]"; try { // 運行存儲過程 db.Database.Connection.Open(); var reader = cmd.ExecuteReader(); // 獲取存儲過程返回數據中的第一個結果 var categories = ((IObjectContextAdapter)db) .ObjectContext .Translate<Category>(reader, "Categories", MergeOption.AppendOnly); lblMsg.Text = "Category Count: " + categories.ToList().Count.ToString(); lblMsg.Text += "<br />"; // 獲取存儲過程返回數據中的第二個結果 reader.NextResult(); var products = ((IObjectContextAdapter)db) .ObjectContext .Translate<Product>(reader, "Products", MergeOption.AppendOnly); lblMsg.Text += "Product Count: " + products.ToList().Count.ToString(); } finally { db.Database.Connection.Close(); } } } } }
3、其他
Index.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="EF50.Index" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <div> <h2>精進不休 .NET 4.5 - ADO.NET Entity Framework 5.0 新特性</h2> <p> 1、<a href="EnumSupport.aspx" target="_blank">對枚舉類型的支持</a> </p> <p> 2、<a href="StoredProcedureWithMultipleResults.aspx" target="_blank">支持通過存儲過程返回多個結果</a> </p> <p> 3、性能提高 </p> <p> 4、新增了空間相關的數據類型 System.Data.Spatial.DbGeography 和 System.Data.Spatial.DbGeometry </p> <p> 5、可以修改實體數據模型中的關系圖的實體的顏色 </p> <p> 6、實體數據模型設計器多了一些功能,通過右鍵屬性看看吧 </p> </div> </body> </html>
二、WCF Data Services 5.0 新特性(OData V3)
服務端
WcfDataService.svc.cs
/* * 提供 OData V3 服務的服務端 */ using System; using System.Collections.Generic; using System.Data.Services; using System.Data.Services.Common; using System.Linq; using System.ServiceModel.Web; using System.Web; namespace ODataV3 { public class WcfDataService : DataService<NorthwindEntities> { public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3; } } }
客戶端
Client.aspx.cs
/* * 調用 OData V3 服務的客戶端 * * 注:關於 WCF Data Services 5.0 的新特性的詳細信息請參見 http://msdn.microsoft.com/en-us/library/hh487257(v=vs.103).aspx */ using ODataV3Client.MyServiceProxy; using System; using System.Collections.Generic; using System.Data.Services.Client; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ODataV3Client { public partial class Client : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Uri uri = new Uri("http://localhost:29702/WcfDataService.svc/"); NorthwindEntities ctx = new NorthwindEntities(uri); /* * 以 URI 語法的方式查詢 ADO.NET 數據服務,詳細語法參看 MSDN * http://[Url]/[ServiceName]/[EntityName]/[NavigationOptions]?[QueryOptions] */ DataServiceQuery<Product> p1 = ctx.CreateQuery<Product>("/Products") .AddQueryOption("$filter", "ProductID eq 10"); // http://localhost:29702/WcfDataService.svc/Products()?$filter=ProductID eq 10 DataServiceQuery<Product> p2 = ctx.CreateQuery<Product>("/Products") .AddQueryOption("$filter", "ProductID ge 10") .AddQueryOption("$top", "3") .AddQueryOption("$orderby", "ProductID asc") .AddQueryOption("$select", "ProductID, ProductName") .Expand("Category"); // 是否需要包括相關的 Category 信息 // http://localhost:29702/WcfDataService.svc/Products()?$expand=Category&$filter=ProductID ge 10&$top=3&$orderby=ProductID asc&$select=ProductID, ProductName DataServiceQuery<Product> p3 = ctx.CreateQuery<Product>("/Products") .AddQueryOption("$filter", "startswith(ProductName, 'a')"); // http://localhost:29702/WcfDataService.svc/Products()?$filter=startswith(ProductName, 'a') /* * 詳細的 URI 語法請參看: * http://msdn.microsoft.com/zh-cn/library/cc668784.aspx * http://msdn.microsoft.com/zh-cn/library/cc668787.aspx * http://msdn.microsoft.com/zh-cn/library/cc668793.aspx */ } } }
OK
[源碼下載]