[索引页]
[源码下载]
作者: webabcd
介绍
以Northwind为示例数据库,ADO.NET Entity Framework之完全面向对象的添加操作、查询操作、更新操作和删除操作
示例
Overview.html
[源码下载]
再接再厉VS 2008 sp1 + .NET 3.5 sp1(1) - Entity Framework(实体框架)之添加、查询、更新和删除的Demo
作者: webabcd
介绍
以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>
<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>
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
{
void Page_Load() 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();
}
}
void btnAddCategory_Click() 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();
}
void GridView1_RowDeleting() 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();
}
void GridView1_SelectedIndexChanged() 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();
}
}
void GridView1_RowUpdating() 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();
}
void GridView1_RowEditing() void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
void GridView1_RowCancelingEdit() void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
}
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
{
void Page_Load() 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();
}
}
void btnAddCategory_Click() 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();
}
void GridView1_RowDeleting() 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();
}
void GridView1_SelectedIndexChanged() 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();
}
}
void GridView1_RowUpdating() 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();
}
void GridView1_RowEditing() void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
void GridView1_RowCancelingEdit() void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
}
OK
[源码下载]
[源码下载]
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/341439
,如需转载请自行联系原作者