第一步:建立数据库
分清楚数据库的主表和从表
建立好两个表的外键关系 然后在两个表添加数据
第二步:打开Visual Studio
建立ASP.NET Web应用程序(.NET Framework)项目
点击项目右键添加新建项
点击ADO.NET实体数据模型
第三步:添加增删改查页面
1.增加Add.aspx页面
右键项目添加Web窗体
2.在Add页面实现代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Add.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<%# Eval("Title")%>
<%# Eval("Author")%>
<%# Eval("Content")%>
<%# Eval("CatelogName")%>
</table>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/>
</div>
</form>
</body>
</html>
3.在Add加载页面中输入代码
点击Button按钮事件
protected void Button1_Click(object sender, EventArgs e)
{
MyDBEntities2 db = new MyDBEntities2();(实例化物理模型)
Article article = new Article();
article.Title = "下雨了";
article.Content = "今天下雨了";
article.Author = "小华";
article.Catelogid = 2;
db.Article.Add(article);
db.SaveChanges();
}
点击Add加载事件
protected void Page_Load(object sender, EventArgs e)
{
MyDBEntities2 db = new MyDBEntities2();
var result = from article in db.Article
join catelog in db.Catelog on article.Catelogid equals catelog.Id
select new { Title = article.Title, Content = article.Content, Author = article.Author, CatelogName = catelog.Name };
this.Repeater1.DataSource = result.ToList();(绑定该表字段)
this.Repeater1.DataBind();
}
EF的新增成功