精進不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性


[索引頁]
[源碼下載] 


精進不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性



作者:webabcd


介紹
精進不休 .NET 4.5

  • ADO.NET Entity Framework 6.0 新特性
  • WCF Data Services 5.6 新特性



示例
1、演示 ADO.NET Entity Framework 6.0 的新特性 - 對 async await 的支持
EF6.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EF6.aspx.cs" Inherits="EF60_DS56.EF6" Async="true" %>

<!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>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

EF6.aspx.cs

/*
 * 演示 ADO.NET Entity Framework 6.0 的新特性 - 對 async await 的支持
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EF60_DS56.Model;
using System.Data.Entity; // 注意:必須要引用此命名空間,它包含了諸如 FirstAsync() ToListAsync() 等異步的擴展方法
using System.Threading.Tasks;

namespace EF60_DS56
{
    public partial class EF6 : System.Web.UI.Page
    {
        // 注意:必須在頁頭中增加 <%@ Async="true" %> 以支持異步
        protected async void Page_Load(object sender, EventArgs e)
        {
            await AsyncDemo1();
            await AsyncDemo2();
        }

        public async Task AsyncDemo1()
        {
            NorthwindEntities context = new NorthwindEntities();
            var product = await context.Products.FirstAsync(); // 此類異步查詢擴展方法來自 System.Data.Entity 命名空間
            product.ProductName = "webabcd " + DateTime.Now.ToString("mm:ss");

            await context.SaveChangesAsync();
        }

        public async Task AsyncDemo2()
        {
            NorthwindEntities context = new NorthwindEntities();
            var product = await context.Products.FirstOrDefaultAsync(); // 此類異步查詢擴展方法來自 System.Data.Entity 命名空間

            Response.Write(product.ProductName);
        }
    }
}


2、演示 WCF Data Services 5.6 的新特性 - 對 ADO.NET Entity Framework 6.0 的支持
WcfDataService.svc.cs

/*
 * 演示 WCF Data Services 5.6 的新特性 - 對 ADO.NET Entity Framework 6.0 的支持
 */

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 EF60_DS56
{
    // DataService<T> - ef5 或以下版本
    // public class WcfDataService : DataService<EF60_DS56.Model.NorthwindEntities> 

    // EntityFrameworkDataService<T> - ef6 或以上版本
    // 注意:在“Install-Package Microsoft.OData.EntityFrameworkProvider -Pre”之后才會有 EntityFrameworkDataService<T>
    public class WcfDataService : System.Data.Services.Providers.EntityFrameworkDataService<EF60_DS56.Model.NorthwindEntities>
    {
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }
    }
}


3、其他
Index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>精進不休 .NET 4.5 - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性</title>
</head>
<body>
    <h2>精進不休 .NET 4.5 - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性</h2>

    <p>
        <a href="EF6.aspx" target="_blank">
            ADO.NET Entity Framework 6.0 新特性 - 支持 async await (示例代碼參見:EF6.aspx.cs)
        </a>
    </p>

    <p>
        <a href="WcfDataService.svc/Products()?$top=1" target="_blank">
            WCF Data Services 5.6 新特性 - 支持 EF6 (示例代碼參見:WcfDataService.svc.cs)
        </a>
    </p>

    <p>
        注:請在 nuget 中搜索 entity framework 6 安裝 ef6
    </p>

    <p>
        注:讓 ds5.6 支持 ef6 需要用到 EntityFrameworkDataService,可以通過 nuget 控制台(工具 -> 庫程序包管理器 -> 程序包管理器控制台)安裝 Install-Package Microsoft.OData.EntityFrameworkProvider -Pre
    </p>
</body>
</html>



OK 
[源碼下載]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM