Example #1
0
 /// <summary>
 /// 根据网站名获取一个网站
 /// 如果存在这样的网站则返回Site对象,如果不存在则返回Null
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public Site GetSite(string name)
 {
     Site site = null;
     try
     {
         using (ACSDbContext db = new ACSDbContext ())
         {
             site = db.Sites.Where(s => s.Name == name).FirstOrDefault();
         }
         return site;
     }
     catch (Exception)
     {
         throw;
     }
 }
Example #2
0
 /// <summary>
 /// 获取名字为name的文章类型
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public szosi.ArticleCollectSystem.Entities.Type GetType(string name)
 {
     szosi.ArticleCollectSystem.Entities.Type type = null;
     try
     {
         using (ACSDbContext db = new ACSDbContext())
         {
             type = db.Types.Where(t => t.Name == name).FirstOrDefault();
         }
         return type;
     }
     catch (Exception)
     {
         throw;
     }
 }
 /// <summary>
 /// 删除一个文章
 /// </summary>
 /// <param name="article"></param>
 /// <returns></returns>
 public bool DelArticle(Article article)
 {
     try
     {
         using (ACSDbContext db = new ACSDbContext ())
         {
             db.Articles.Attach(article);
             db.Articles.Remove(article);
             db.SaveChanges();
         }
         return true;
     }
     catch (Exception)
     {
         return false;
     }
 }
Example #4
0
        private bool disposedValue = false; // 要检测冗余调用

        #endregion Fields

        #region Methods

        /// <summary>
        /// 添加一个新网站
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        public bool AddSite(Site site)
        {
            try
            {
                using (ACSDbContext db = new ACSDbContext())
                {
                    db.Sites.Attach(site);
                    db.Sites.Add(site);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Example #5
0
        private bool disposedValue = false; // 要检测冗余调用

        #endregion Fields

        #region Methods

        /// <summary>
        /// 添加一个文章类型对象
        /// </summary>
        /// <param name="type"></param>
        /// <returns>返回是否添加成功</returns>
        public bool AddType(szosi.ArticleCollectSystem.Entities.Type type)
        {
            try
            {
                using (ACSDbContext db = new ACSDbContext ())
                {
                    db.Types.Attach(type);
                    db.Types.Add(type);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
 /// <summary>
 /// 获取所有的文章列表 
 /// 按时间排序
 /// </summary>
 /// <returns></returns>
 public List<Article> GetArticleList()
 {
     List<Article> articleList = null;
     try
     {
         using (ACSDbContext db = new ACSDbContext())
         {
             //
             //当要生成外键实体时 查询的时候加上.Include(a=>a.外键实体名)即可
             //在使用.include()时,要记得using System.Data.Entity;
             articleList =
                 db.Articles.Include(a => a.Site).Include(a=>a.Type)
                 .OrderByDescending(a => a.Time).ToList();
         }
         return articleList;
     }
     catch (Exception)
     {
         throw;
     }
 }
        public bool UpdateArticle(Article article)
        {
            try
            {
                using (ACSDbContext db = new ACSDbContext())
                {
                    DbEntityEntry<Article> entry = db.Entry<Article>(article);

                    entry.State = EntityState.Unchanged;
                    entry.Property("Title").IsModified = true;
                    entry.Property("Content").IsModified = true;
                    entry.Property("Time").IsModified = true;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Example #8
0
 /// <summary>
 /// 获取网站列表
 /// </summary>
 /// <returns></returns>
 public List<Site> GetSiteList()
 {
     List<Site> siteList = null;
     try
     {
         using (ACSDbContext db = new ACSDbContext ())
         {
             siteList = db.Sites.ToList();
         }
         return siteList;
     }
     catch (Exception)
     {
         throw;
     }
 }
Example #9
0
        /// <summary>
        /// 更新网站地址
        /// </summary>
        /// <param name="site"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public bool UpdateSiteAddress(Site site, string address)
        {
            try
            {
                site.Address = address;
                // 这种更新数据的方法不用先查询,直接修改
                using (ACSDbContext db = new ACSDbContext())
                {
                    DbEntityEntry<Site> entry = db.Entry<Site>(site);

                    entry.State = EntityState.Unchanged;
                    entry.Property("Address").IsModified = true;

                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #10
0
 /// <summary>
 /// 获得类型列表
 /// </summary>
 /// <returns></returns>
 public List<szosi.ArticleCollectSystem.Entities.Type> GetTypeList()
 {
     List<szosi.ArticleCollectSystem.Entities.Type> typeList = null;
     try
     {
         using (ACSDbContext db = new ACSDbContext ())
         {
             typeList = db.Types.ToList();
         }
         return typeList;
     }
     catch (Exception)
     {
         throw;
     }
 }