zoukankan      html  css  js  c++  java
  • 精进不休 .NET 4.5 (11) ADO.NET Entity Framework 5.0 新特性, WCF Data Services 5.0 新特性(OData V3)

    [索引页]
    [源码下载] 


    精进不休 .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 
    [源码下载]

  • 相关阅读:
    KVM 核心功能:磁盘虚拟化
    KVM 核心功能:内存虚拟化
    KVM 核心功能:CPU 虚拟化
    OpenStack 工作流组件: Mistral
    QT 5 种标准对话框使用方法,及实现效果,之二 —— 颜色对话框(QColorDialog)
    Git 安装
    在多人共同开发一个项目中使用 Git 的简单流程
    QT 5 种标准对话框使用方法,及实现效果,之一 —— 文件对话框(QFileDialog)
    Spark安装
    DATAFUN-推荐算法
  • 原文地址:https://www.cnblogs.com/webabcd/p/2723735.html
Copyright © 2011-2022 走看看