EF框架实现增删改查
1.创建数据库:先创建两张表,文章类型表以及文章详情表,设置主外键(ID,Catelogid)
Catelog:文章类型表
字段:Id(自增),Name,[Content]
| 列名 | 数据类型 |
| Id | int |
| Name | varchar(50) |
| [Content] | text |
Article:文章详情表
字段:Id(自增),Title,Author,[Content],Catelogid
| 列名 | 数据类型 |
| Id | int |
| Title | varchar(50) |
| Author | varchar(50) |
| [Content] | text |
| Catelogid | int |
2.创建ASP.NET Web 应用程序(.NET Framework)
创建一个DAO.NET实体数据模型--->来自数据库的EF设计器--->把自己要用的表添加进来(Catelog,Article)
3.给一个web窗体让数据显示出来

实现代码如下:
<form id="form1" runat="server" ><div class="d1"><p style="text-align:center; padding-right:50px;">添加信息</p>标 题:<asp:TextBox ID="txttitle" runat="server"></asp:TextBox><br />作 者 : <asp:TextBox ID="txtauthor" runat="server"></asp:TextBox><br /> 内 容:<asp:TextBox ID="txtcontent" runat="server"></asp:TextBox><br />类 型:<asp:TextBox ID="txtcatelog" runat="server"></asp:TextBox><br />类 型 描 述:<asp:TextBox ID="txtcatecontent" runat="server"></asp:TextBox><br /> <asp:Button ID="Button2" runat="server" Text="添加" OnClick="Button2_Click" Height="27px" Width="89px" CssClass="d3" /><br /> </div><div class="d2"><table border="1"><tr><td style="width:150px">标题</td><td style="width:80px">作者</td><td style="width:150px">内容</td><td style="width:80px">类型名称</td></tr><asp:Repeater ID="Repeater1" runat="server"><ItemTemplate><tr><td><%#Eval("Title") %></td><td><%#Eval("Author") %></td><td><%#Eval("Content") %></td><td><%#Eval("Catelogid") %></td> </tr> </ItemTemplate></asp:Repeater></table></div></form>
我对页面的美观要求比较高,下面是我的页面样式
<style>.d1 {width:350px;height:220px;margin-left:20px;border:1px cadetblue solid;padding-left:50px;background-color:aliceblue}.d2 {margin:auto;text-align:center;margin-top:10px;}.d3 {margin-left:25%;background-color:gainsboro;border:1px solid black;margin-top:10px;}.d2 table {text-align:center;}</style>
在窗体的加载事件中,让想要显示的字段显示出来
用了DAO.NET实体数据模型已经把增删改查的方法封装好了,只需要调用MyDBEntities即可,相对于以前方便了很多
想要页面显示什么字段就查询什么字段,然后绑定到Repeater中
protected void Page_Load(object sender, EventArgs e){MyDBEntities1 db = new MyDBEntities1();var result = from article in db.Articlejoin catelog in db.Catelog on article.Catelogid equals catelog.Idselect new { Title = article.Title, Author = article.Author, Content = article.Content, Catelogid = catelog.Name };this.Repeater1.DataSource = result.ToList();this.Repeater1.DataBind();}
点击添加按钮实现添加以及页面重新加载代码如下:
protected void Button2_Click(object sender, EventArgs e){MyDBEntities1 db = new MyDBEntities1();//同时加两个表Article art = new Article();art.Title = txttitle.Text;art.Content = txtcontent.Text;art.Author = txtauthor.Text;//article.Catelogid = 1;//添加指定的类型//新增类型art.Catelog = new Catelog { Name = txtcatelog.Text, Content = txtcatecontent.Text };//直接调用ef封装好的添加方法db.Article.Add(art);//实时更新到物理数据库db.SaveChanges();int count = db.SaveChanges();if (count >= 0){Response.Write("添加成功");var result = from article in db.Articlejoin catelog in db.Catelog on article.Catelogid equals catelog.Idselect new { Title = article.Title, Author = article.Author, Content = article.Content, Catelogid = catelog.Name };this.Repeater1.DataSource = result.ToList();this.Repeater1.DataBind();}}
可以通过想查询的id 进行查询,代码如下:
protected void Page_Load(object sender, EventArgs e){MyDBEntities1 db = new MyDBEntities1();List<Article> articles = new List<Article>(); //创建一个新的集合对象articles.Add(db.Article.FirstOrDefault(p => p.Id == 4)); //根据你想要查询的id进行查询this.Repeater1.DataSource = articles; //Repeater1绑定值,数据绑定this.Repeater1.DataBind(); //数据显示}















