标签:style blog http color 使用 os strong io
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差别很大。这里记录一下,使用起来的时候,希望能快速上手。
标签:style blog http color 使用 os strong io
原文地址:http://www.cnblogs.com/wolf-sun/p/3873859.html