11-資料訪問層
public class NewsDal
{
DBcontext db = new DBcontext();
/// <summary>
/// 檢視釋出的所有資訊
/// </summary>
/// <returns></returns>
public List<News> GetNews()
{
return db.News.ToList();
}
/// <summary>
/// 添加發布資訊
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public int AddNews(News n)
{
db.News.Add(n);
return db.SaveChanges();
}
/// <summary>
/// 根據ID查一個
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public News GetNew(int id)
{
return db.News.FirstOrDefault(n => n.ID.Equals(id));
}
/// <summary>
/// 修改釋出資訊
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public int UptNew(News n)
{
db.Entry(n).State = System.Data.Entity.EntityState.Modified;
return db.SaveChanges();
}
}
public class CommentDAL
{
DBcontext db = new DBcontext();
/// <summary>
/// 顯示評論資訊
/// </summary>
/// <returns></returns>
public List<Comment> GetComments()
{
return db.Comments.ToList();
}
/// <summary>
/// 新增評論資訊
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public int AddComment(Comment c)
{
db.Comments.Add(c);
return db.SaveChanges();
}
}