使用EntityFramework在论坛项目上添加文章
基本步骤
- 使用Visual Studio 新建 ASP.NET 应用程序(.NET Framework)项目;
- 添加一个添加窗体(Add.aspx);
- 在解决方案中创建Model、DAL、BLL三层类库
- 在Model层中添加一个 ADO.NET 实体数据模型 连接数据库中的表(Model1.edmx);
- 在DAL层中创建两个类分别用来使用数据库中的两个表的数据
- 在BLL层中创建两个类来编写业务逻辑代码;
创建Add.aspx窗体,并添加控件
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<div class="row">
<div class="cols-4">标题:</div>
<div class="cols-8">
<asp:TextBox ID="TxtTitle" runat="server" CssClass="control-width"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="cols-4">作者:</div>
<div class="cols-8">
<asp:TextBox ID="TxtAuthor" runat="server" CssClass="control-width"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="cols-4">内容:</div>
<div class="cols-8">
<asp:TextBox ID="TxtContent" runat="server" TextMode="MultiLine" CssClass="control-width"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="cols-4">类别:</div>
<div class="cols-8">
<asp:DropDownList ID="DdlCatelog" runat="server" CssClass="control-width" DataTextField="Name" DataValueField="Id"></asp:DropDownList>
</div>
</div>
<div class="row">
<div class="cols-4"></div>
<div class="cols-8">
<asp:Button ID="Button1" runat="server" Text="发表文章" OnClick="Button1_Click" />
</div>
</div>
</div>
</form>
创建Model、DAL、BLL三层类库
创建三层架构并且分别在每一层之中添加类、并在三层架构中逐层添加引用
并且在Model层中添加 ADO.NET 实体数据模型 连接数据库(Model1.edmx)
在DAL层的两个类中编写代码
在ArticleDAO.cs 中编写查询和添加语句
public class ArticleDAO
{
public List<Article> Select()
{
MyDBEntities db = new MyDBEntities();
return db.Article.Select(a=>a).ToList();
}
public Article Select(int id)
{
MyDBEntities db = new MyDBEntities();
return db.Article.Where(p=>p.Id==id).FirstOrDefault();
}
public int Add(Article article)
{
MyDBEntities db = new MyDBEntities();
db.Article.Add(article);
db.Entry<Catelog>(article.Catelog).State = System.Data.Entity.EntityState.Unchanged;
return db.SaveChanges();
}
}
在 CatelogDAO.cs 中编写两表连接的查询
public class CatelogDAO
{
{
MyDBEntities db = new MyDBEntities();
// return db.Catelog.Select(p=>p).ToList();
var result = from catelog in db.Catelog
select catelog;
return result.ToList();
}
}
在BLL层的两个类中编写业务逻辑代码
在 ArticleService.cs 中编写第一个表中数据的逻辑代码
public class ArticleService
{
private ArticleDAO adao = new ArticleDAO();
public List<Article> Select()
{
return adao.Select();
}
public Article Select(int id)
{
return adao.Select(id);
}
public int AddArticle(Article article)
{
if (String.IsNullOrEmpty(article.Title)){
throw new Exception("标题不能为空!");
}
return adao.Add(article);
}
}
在 CatelogServcie.cs 中编写两表连接查询的逻辑代码
public class CatelogServcie
{
private CatelogDAO cdao = new CatelogDAO();
/// <summary>
/// 所有的类别
/// </summary>
/// <returns></returns>
public List<Catelog> Select()
{
return cdao.Select();
}
}
编写Add.aspx.cs中的代码
将BLL层中的两个类进行调用和实例化
private CatelogServcie catelogService = new CatelogServcie();
private ArticleService articleService = new ArticleService();
在视图层中绑定数据
protected void Page_Load(object sender, EventArgs e)
{
string message=Request.QueryString["message"];
if(!string.IsNullOrEmpty(message))
this.Label1.Text = message;
if (!IsPostBack)
{
this.DdlCatelog.DataSource = catelogService.Select();
this.DdlCatelog.DataBind();
}
}
通过双击 Button 按钮控件进入Add.aspx.cs中编写添加文章的代码
protected void Button1_Click(object sender, EventArgs e)
{
Article article = new Article();
article.Title = this.TxtTitle.Text;
article.Author = this.TxtAuthor.Text;
article.Content = this.TxtContent.Text;
article.PushTime = DateTime.Now;
//article.Catelogid = int.Parse(this.DdlCatelog.SelectedValue);
article.Catelog = new Catelog() { Id= int.Parse(this.DdlCatelog.SelectedValue) };
try
{
int count = articleService.AddArticle(article);
if (count > 0)
{
Response.Redirect("~/Add.aspx?message=添加成功!");
}
}
catch (Exception ex)
{
Response.Redirect("~/Add.aspx?message="+ex.Message);
}
}
整个Add.aspx.cs中的整体代码如下:
public partial class Add : System.Web.UI.Page
{
private CatelogServcie catelogService = new CatelogServcie();
private ArticleService articleService = new ArticleService();
protected void Page_Load(object sender, EventArgs e)
{
string message=Request.QueryString["message"];
if(!string.IsNullOrEmpty(message))
this.Label1.Text = message;
if (!IsPostBack)
{
this.DdlCatelog.DataSource = catelogService.Select();
this.DdlCatelog.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Article article = new Article();
article.Title = this.TxtTitle.Text;
article.Author = this.TxtAuthor.Text;
article.Content = this.TxtContent.Text;
article.PushTime = DateTime.Now;
//article.Catelogid = int.Parse(this.DdlCatelog.SelectedValue);
article.Catelog = new Catelog() { Id= int.Parse(this.DdlCatelog.SelectedValue) };
try
{
int count = articleService.AddArticle(article);
if (count > 0)
{
Response.Redirect("~/Add.aspx?message=添加成功!");
}
}
catch (Exception ex)
{
Response.Redirect("~/Add.aspx?message="+ex.Message);
}
}
}
总结:
不管是单表的增删改查还是多表增删改查,其性质都是差不多的,就多表的增删改查中多出了一个表的连接