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

2023-06-05,,

[索引页]
[源码下载]

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

精进不休 .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 新特性.doc》

下载本文的Word格式文档,以方便收藏与打印。