EntityFrameWork.doc_第1页
EntityFrameWork.doc_第2页
EntityFrameWork.doc_第3页
EntityFrameWork.doc_第4页
EntityFrameWork.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、首先创建两张表,产品表T_Product和T_ProClass在这里,我们把两张表的关系,主外键设置好二、现在我们通过vs2012添加Entity Framework选择ADO.NET实体数据模型,添加选择从数据库生成连接成功单击完成后,会生成如图所示的实体模型我们可以看到在项目中生成了两个文件Model.edmx和Model.Designer.cs这两个文件分别用来描述实体以及他们之间关系,和自动生成的C#代码,包括实体类的代码以及他们之间的代码三、框架已经搭建好,现在我们就可以通过linq语法实现对数据的查询,修改,删除,增加等操作了首先,我们来查询一下产品的列表在页面中使用Repeat控件用来显示数据 产品ID 产品名?称? 产品价?格? 产品类别e 代码很简单,就是一个简单的查询public partial class Index : System.Web.UI.Page EntityDataEntities ef = new EntityDataEntities(); protected void Page_Load(object sender, EventArgs e) if(!IsPostBack) ProBind(); public void ProBind() var proList = from s in ef.T_Product select s; Repeater1.DataSource = proList; Repeater1.DataBind(); 在这里我们要重点说一下,因为数据库的主外键关系已经被映射到对象中,所以我们可以通过对象直接调用和它关联的对象,在这里就是可以通过产品T_Product直接调用类别T_ProClass好,现在我们在来看看如何添加产品在页面中添加文本框和按钮 产品名?称? 产品价?格? 产品类别e 后台代码public partial class Insert : System.Web.UI.Page EntityDataEntities ef = new EntityDataEntities(); protected void Page_Load(object sender, EventArgs e) if(!IsPostBack) bindProClass(); protected void bindProClass() var query = from ps in ef.T_ProClass select ps; RadioButtonList1.DataSource = query; RadioButtonList1.DataTextField = proClassName; RadioButtonList1.DataValueField = ID; RadioButtonList1.DataBind(); protected void Button1_Click(object sender, EventArgs e) T_Product product = new T_Product(); product.ProClass =Convert.ToInt32(RadioButtonList1.SelectedValue); product.ProName = txtProName.Text; product.ProPrice = Convert.ToDecimal(txtProPrice.Text); ef.T_Product.AddObject(product); ef.SaveChanges(); ef.Dispose(); Response.Write(alert(添加成功|);location.href=Index.aspx); 修改和添加很类似页面代码: 产品名?称? 产品价?格? 产品类别e 后台的代码 EntityDataEntities ef = new EntityDataEntities(); protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) int id = Convert.ToInt32(Request.QueryStringproId.ToString(); bindProClass(); ProdectById(id); protected void bindProClass() var query = from ps in ef.T_ProClass select ps; RadioButtonList1.DataSource = query; RadioButtonList1.DataTextField = proClassName; RadioButtonList1.DataValueField = ID; RadioButtonList1.DataBind(); protected void ProdectById(int id) var product = (from p in ef.T_Product where p.ID = id select p).Single(); RadioButtonList1.SelectedValue = product.ProClass.ToString(); txtProName.Text = product.ProName; txtProPrice.Text = product.ProPrice.ToString(); protected void Button1_Click(object sender, EventArgs e) int id = Convert.ToInt32(Request.QueryStringproId.ToString(); var product = (from p in ef.T_Product where p.ID = id select p).Single(); product.ProClass = Convert.ToInt32(RadioButtonList1.SelectedValue); product.ProName = txtProName.Text; product.ProPrice = Convert.ToDecimal(txtProPrice.Text); ef.SaveChanges(); ef.Dispose(); Response.Write(alert(修T改?成功|);location.href=Index.aspx); 删除的代码 EntityDataEntities ef = new EntityDataEntities(); protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) int id = Convert.ToInt32(Request.QueryStringproId.ToString(); var product = (from p in ef.T_Product where p.ID = id s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论