Entity Framework(實體框架)之添加、查詢、更新和刪除的Demo
介紹
以Northwind為示例數據庫,ADO.NET Entity Framework之完全面向對象的添加操作、查詢操作、更新 操作和刪除操作
示例
Overview.html
<ul> <li>ADO.NET Entity Framework(EF) - 就當是微軟的ORM吧,可以將概念模型映射到邏輯模型 ,從而使用概念模型做開發 </li> <li>Entity - 在EF中就是實體的概念 </li> <li>Entity Data Model(EDM) - 實體數據模型,表現為一組映射到數據源的實體和關系 </li> <li>LINQ to Entities - 和LINQ to XXX一樣,對EF中的實體做LINQ</li> <li>Entity SQL(ESQL) - 一種類似sql的查詢語言,針對概念模型做查詢</li> <li>csdl - Conceptual schema definition language 概念架構定義語言</li> <li>ssdl - Store schema definition language 存儲架構定義語言 </li> <li>msl - Mapping specification language 映射規范語言</li> <li>csdl, ssdl, msl均為基於XML的語言。.edmx文件同時包含這3種語言所描述的信息 </li> </ul> <ul> <li>Entity - 實體</li> <li>Entity Class - 實體類。至少要有一個實體鍵(Key Properties)</li> <li>Entity Set - 實體集。實體集中包含著若干實體</li> <li>Association - 關聯。實體類之間的關系。導航屬性就是由關聯來定義的</li> <li>Association Set - 關聯集。關聯集中包含著若干關聯</li> <li>Entity Container - 實體容器。實體容器中包含著若干實體集和關聯集</li> </ul> <ul> <li>概念模型中必須要有實體鍵,實體鍵與表中的主鍵相對應,所以表中必須要有主鍵 </li> <li>存儲模型的字段如果有主鍵鍵或非空約束,則概念模型中必需要有相應的屬性與之映射 </li> <li>概念模型中存在的屬性,必須在存儲模型中有與之映射的字段</li> <li>作為映射條件的字段不能映射到屬性上</li> <li>要實現繼承的話,就要使用映射條件來進行篩選</li> </ul> <ul> <li>EntityConnection - 與存儲模型的連接。相當於SqlConnection</li> <li>EntityCommand - 對 EDM 執行的命令。相當於SqlCommand</li> <li>EntityDataReader - 以只讀、只進的數據流的方式獲取數據(內存中始終只有一行)。 相當於SqlDataReader</li> </ul>
Demo.aspx
<%@ Page Title="添加、查詢、更新和刪除的Demo" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Demo.aspx.cs" Inherits="EntityFramework_Demo" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <div style="margin: 10px;"> CategoryName:<asp:TextBox ID="txtCategoryName" runat="server" /> Description:<asp:TextBox ID="txtDescription" runat="server" /> <asp:Button ID="btnAddCategory" runat="server" Text="添加產品類別" OnClick="btnAddCategory_Click" /> </div> <div style="margin: 10px;"> <asp:GridView ID="GridView1" DataKeyNames="CategoryID" runat="server" AutoGenerateColumns="false" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="CategoryID" HeaderText="ID" ReadOnly="true" /> <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" /> <asp:BoundField DataField="Description" HeaderText="Description" /> <asp:CommandField ShowSelectButton="True" ShowEditButton="True" ShowDeleteButton="True"> </asp:CommandField> </Columns> </asp:GridView> </div> <div style="margin: 10px;"> <asp:GridView ID="GridView2" runat="server"> </asp:GridView> </div> </asp:Content>
Demo.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// 在 System.Data.Entity.dll 程序集中
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using VS2008SP1.Business;
public partial class EntityFramework_Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
using (var ctx = new NorthwindEntities())
{
// 將 System.Data.Objects.ObjectQuery<Categories> 綁定到 Gridview1
GridView1.DataSource = ctx.Categories;
GridView1.DataBind();
}
}
protected void btnAddCategory_Click(object sender, EventArgs e)
{
// NorthwindEntities - 繼承自 System.Data.Objects.ObjectContext
// ObjectContext - 以對象(這些對象是 EDM 中定義的實體類型的實例)的形式與數據進行
交互
using (var ctx = new NorthwindEntities())
{
// CreateObjectName - 實體類 的 CreateObjectName 靜態方法用於創建實體類的新實例
// 構造函數的參數為對應的實體類的必填字段
// 如果參數屬於自增列,如本例中的 categoryID ,可以先對其賦予任意值
var category = Categories.CreateCategories(-1, txtCategoryName.Text);
category.Description = txtDescription.Text;
for (int i = 0; i < 10; i++)
{
category.Products.Add(new Products() { ProductName = "測試用" + i.ToString
()});
}
// AddToEntitySetName() - 將需要添加的對象添加到對象上下文中
ctx.AddToCategories(category);
// SaveChanges() - 將所有更新保存到相關存儲區中
ctx.SaveChanges();
// SaveChanges() 之後就可以通過 category.CategoryID 來取得其自增列在數據庫中所保
存的值
Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "alert
('CategoryID: " + category.CategoryID + "');", true);
}
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
using (var ctx = new NorthwindEntities())
{
var categoryId = (int)GridView1.DataKeys[e.RowIndex].Value;
var category = ctx.Categories.First(p => p.CategoryID == categoryId);
// 加載類別下的產品到當前上下文中,用以級聯刪除
// 當然如果在數據庫中配置了級聯刪除,並且映射中配置了<OnDelete
Action="Cascade" />(EDM編輯器會根據數據庫中的配置自動做此配置)則不用此句
category.Products.Load();
// DeleteObject() - 將指定的對象的狀態更改為已刪除(調用 SaveChanges() 之後才會
執行)
ctx.DeleteObject(category);
ctx.SaveChanges();
}
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
using (var ctx = new NorthwindEntities())
{
var categoryId = (int)GridView1.SelectedDataKey.Value;
// 查找當前選中的類別下的產品集合
var products = ctx.Products.Where(p => p.Categories.CategoryID ==
categoryId);
// 將 System.Linq.IQueryable<Products> 綁定到 GridView2
GridView2.DataSource = products;
GridView2.DataBind();
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
using (var ctx = new NorthwindEntities())
{
var categoryId = (int)GridView1.DataKeys[e.RowIndex].Value;
var category = ctx.Categories.First(p => p.CategoryID == categoryId);
// 修改 category 對象的屬性
category.CategoryName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls
[0]).Text;
category.Description = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls
[0]).Text;
ctx.SaveChanges();
}
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
}
本文配套源碼:http://www.bianceng.net/dotnet/201212/805.htm