Esempio n. 1
0
        public int Insert(Infrastructure.Data.Entities.NewsVm news)
        {
            try
            {
                News _news = new News
                {
                    NewsTitle       = news.NewsTitle,
                    NewsDescription = news.NewsDescription,
                    NewsDate        = news.NewsDate,
                    NewsExpiryDate  = news.NewsExpiryDate,
                    NewsBannerPath  = news.NewsBannerPath,
                    NewsIconPath    = news.NewsIconPath,
                    NewsVideoPath   = news.NewsVideoPath,
                    NewsLangauage   = news.NewsLangauage,
                    PostUserID      = news.PostUserID,
                    PostDate        = DateTime.Now,
                    IsInformal      = news.IsInformal,
                    IsAchievement   = news.IsAchievement
                };
                int affectedRows = 0;
                using (var db = new LMISEntities())
                {
                    if (news.NewsID < 1)
                    {
                        db.News.Add(_news);
                    }
                    else
                    {
                        var _News = (from n in db.News where n.NewsID == news.NewsID select n).FirstOrDefault();
                        _News.NewsTitle       = news.NewsTitle;
                        _News.NewsDescription = news.NewsDescription;
                        _News.NewsDate        = news.NewsDate;
                        _News.NewsExpiryDate  = news.NewsExpiryDate;
                        _News.IsInformal      = news.IsInformal;
                        _News.IsAchievement   = news.IsAchievement;
                        if (!string.IsNullOrEmpty(news.NewsBannerPath))
                        {
                            _News.NewsBannerPath = news.NewsBannerPath;
                        }
                        if (!string.IsNullOrEmpty(news.NewsIconPath))
                        {
                            _News.NewsIconPath = news.NewsIconPath;
                        }
                        if (!string.IsNullOrEmpty(news.NewsVideoPath))
                        {
                            _News.NewsVideoPath = news.NewsVideoPath;
                        }
                        db.News.Attach(_News);
                        db.Entry(_News).State = EntityState.Modified;
                    }

                    db.SaveChanges();

                    affectedRows = int.Parse(_news.NewsID.ToString());
                }
                return(affectedRows);
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            catch (DbUnexpectedValidationException ex)
            {
                Console.WriteLine(ex.StackTrace + ":" + ex.InnerException);
                throw;
            }
        }
Esempio n. 2
0
 public int Update(Infrastructure.Data.Entities.NewsVm News)
 {
     throw new NotImplementedException();
 }