目錄
認識EF
ADO.NET Entity Framework 是微軟以ADO.NET為基礎所發展出來的對象關系對伊(O/R Mapping)解決方案,早起被稱為ObjectSpage,最新版本EF6。
實體框架Entity Framework是ADO.NET中的一組支持面向數據的軟件應用程序的技術。是微軟的一個ORM框架。
什么是O/R Mapping
廣義上,ORM指的是面向對象模型和關系數據庫的數據結構之間的相互轉換。
狹義上,ORM可以被認為是,基於關系數據庫的數據存儲,實現一個虛擬的面向對象的數據訪問接口。理想情況下,基於這樣一個面向對象的接口,持久化一個OO對象應該不需要了解任何關系型數據存儲數據的實現細節。
添加數據
測試用數據庫

1 USE [Wolfy.Shop] 2 GO 3 4 /****** Object: Table [dbo].[TB_Customer] Script Date: 2014/7/29 20:01:56 ******/ 5 SET ANSI_NULLS ON 6 GO 7 8 SET QUOTED_IDENTIFIER ON 9 GO 10 11 CREATE TABLE [dbo].[TB_Customer]( 12 [ID] [uniqueidentifier] NOT NULL, 13 [Name] [nvarchar](32) NULL, 14 [Address] [nvarchar](100) NULL, 15 [Gender] [bit] NULL, 16 CONSTRAINT [PK_CustomerID] PRIMARY KEY CLUSTERED 17 ( 18 [ID] ASC 19 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 20 ) ON [PRIMARY] 21 22 GO

1 USE [Wolfy.Shop] 2 GO 3 4 /****** Object: Table [dbo].[TB_Order] Script Date: 2014/7/29 20:02:47 ******/ 5 SET ANSI_NULLS ON 6 GO 7 8 SET QUOTED_IDENTIFIER ON 9 GO 10 11 CREATE TABLE [dbo].[TB_Order]( 12 [ID] [int] NOT NULL, 13 [OrderName] [nvarchar](1000) NULL, 14 [CreateDate] [datetime] NULL, 15 [customerId] [uniqueidentifier] NULL, 16 CONSTRAINT [PK_TB_Order] PRIMARY KEY CLUSTERED 17 ( 18 [ID] ASC 19 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 20 ) ON [PRIMARY] 21 22 GO 23 24 ALTER TABLE [dbo].[TB_Order] WITH CHECK ADD CONSTRAINT [FK_Customer_Order] FOREIGN KEY([customerId]) 25 REFERENCES [dbo].[TB_Customer] ([ID]) 26 GO 27 28 ALTER TABLE [dbo].[TB_Order] CHECK CONSTRAINT [FK_Customer_Order] 29 GO
工具VS2013,SQL SERVER2012
在使用EF的時候發現,跟之前的版本差別還是挺大的,有些方法的名字都改了。這里記錄一下,使用的時候方便查找吧。
測試頁面

1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddCustomer.aspx.cs" Inherits="Wofly.EFDemo.AddCustomer" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 <title></title> 9 </head> 10 <body> 11 <form id="form1" runat="server"> 12 <div> 13 <asp:Button Text="添加" CommandArgument="add" runat="server" ID="btnAdd" OnClick="btnAdd_Click" /> 14 <table> 15 <tr> 16 <td>用戶名:</td> 17 <td> 18 <asp:TextBox runat="server" ID="txtUserName" ClientIDMode="Static" /></td> 19 </tr> 20 <tr> 21 <td>用戶地址:</td> 22 <td> 23 <asp:TextBox runat="server" ID="txtAddress" ClientIDMode="Static" /></td> 24 </tr> 25 <tr> 26 <td>性別:</td> 27 <td> 28 <asp:RadioButton runat="server" Checked="true" ID="rdbMan" Text="男" GroupName="gender" /> 29 <asp:RadioButton GroupName="gender" runat="server" ID="rdbFemale" Text="女" /> 30 </td> 31 </tr> 32 </table> 33 <asp:Repeater runat="server" ID="rptCustomerList"> 34 <HeaderTemplate> 35 <table> 36 <tr> 37 <th>序號</th> 38 <th>用戶名</th> 39 <th>地址</th> 40 <th>性別</th> 41 <th>操作</th> 42 </tr> 43 </HeaderTemplate> 44 <ItemTemplate> 45 <tr> 46 <td><%#Container.ItemIndex+1 %></td> 47 <td><%#Eval("Name") %></td> 48 <td><%#Eval("Address") %></td> 49 <td><%#Convert.ToBoolean(Eval("Gender"))==true?"男":"女" %></td> 50 <td> 51 <asp:LinkButton Text="編輯" runat="server" OnClick="lnkEdit_Click" CommandName="Edit" CommandArgument='<%#Eval("ID") %>' ID="lnkEdit" /><asp:LinkButton Text="刪除" runat="server" OnClick="lnkEdit_Click" ID="lnkDelete" CommandName="Delete" CommandArgument='<%#Eval("ID") %>' /></td> 52 </tr> 53 </ItemTemplate> 54 <FooterTemplate> 55 </table> 56 </FooterTemplate> 57 </asp:Repeater> 58 </div> 59 </form> 60 </body> 61 </html>
增加用戶
1 ShopEntities shopEntities = new ShopEntities(); 2 TB_Customer tb_CustomerAdd = new TB_Customer() { ID = Guid.NewGuid(), Name = txtUserName.Text, Address = txtAddress.Text, Gender = rdbMan.Checked ? true : false }; 3 shopEntities.TB_Customer.Add(tb_CustomerAdd); 4 int intResult = shopEntities.SaveChanges(); 5 if (intResult > 0) 6 { 7 DataInit(); 8 }
刪除數據
1 shopEntities.TB_Customer.Remove(tb_Customer); 2 if (shopEntities.SaveChanges() > 0) 3 { 4 this.DataInit(); 5 }
修改數據
1 ShopEntities shopEntities = new ShopEntities(); 2 var customer = from c in shopEntities.TB_Customer 3 where c.ID == new Guid(btn.CommandArgument) 4 select c; 5 TB_Customer tb_CustomerUpdate = customer.FirstOrDefault<TB_Customer>(); 6 //將 對象 添加到 EF中 7 tb_CustomerUpdate.Name = txtUserName.Text; 8 tb_CustomerUpdate.Address = txtAddress.Text; 9 tb_CustomerUpdate.Gender = rdbMan.Checked ? true : false; 10 //是否修改 11 shopEntities.Entry<TB_Customer>(tb_CustomerUpdate).State = System.Data.EntityState.Modified; 12 //一次性 生成sql語句到數據庫執行 13 shopEntities.SaveChanges();
查詢數據
1 private void DataInit() 2 { 3 ShopEntities shopEntities = new ShopEntities(); 4 var customers = from c in shopEntities.TB_Customer 5 select c; 6 this.rptCustomerList.DataSource = customers.ToList(); 7 this.rptCustomerList.DataBind(); 8 }
總結
新技術更新可真快,這里只是記錄增刪改查的方法,也沒具體描述,只是影響中感覺跟ef4差別很大。這里記錄一下,使用起來的時候,希望能快速上手。