Пример #1
0
        public T Add(T entity)
        {
            var result = dbContext.Set <T>().Add(entity);

            dbContext.SaveChanges();
            return(result);
        }
Пример #2
0
 public ActionResult Create(Article a)
 {
     if (ModelState.IsValid) //如果数据符合要求 ( 按照规则校验)
     {
         db.Articles.Add(a);
         db.SaveChanges();
     }
     return(RedirectToAction("index"));
 }
Пример #3
0
        public ActionResult Create([Bind(Include = "UserId,UserName,Password,Gender,Email,SecurityQuestion,SecurityAnswer,QQ,Tel,Address,PostCode,RegTime,LastLoginTime")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(user));
        }
Пример #4
0
        public ActionResult Create([Bind(Include = "CategoryId,CategoryName")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categorys.Add(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(category));
        }
Пример #5
0
        public ActionResult Create([Bind(Include = "Id,Title,Producer,Date,Category,DEscription")] News news)
        {
            if (ModelState.IsValid)
            {
                db.News.Add(news);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(news));
        }
Пример #6
0
        public ActionResult Create([Bind(Include = "ID,Title,ReleaseDate,Text")] NewsViewModel newsViewModel)
        {
            if (ModelState.IsValid)
            {
                db.News.Add(newsViewModel);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(newsViewModel));
        }
Пример #7
0
        public ActionResult Create([Bind(Include = "Id,TimeStamp,Text")] NewsItem newsItem)
        {
            if (ModelState.IsValid)
            {
                db.News.Add(newsItem);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(newsItem));
        }
Пример #8
0
        static void Main()
        {
            var context    = new NewsDBContext();
            var newContext = new NewsDBContext();
            var firstNews  = context.News.FirstOrDefault();

            Console.WriteLine("Application started");
            Console.Write("Text from DB: ");
            Console.WriteLine(firstNews.NewsContent);
            Console.WriteLine("Enter the corrected text: ");


            firstNews.NewsContent = Console.ReadLine();

            try
            {
                context.SaveChanges();

                Console.WriteLine("Changes successfully saved in the DB.");
            }
            catch (DbUpdateConcurrencyException ex)
            {
                var textFromDB = newContext.News.FirstOrDefault();
                Console.WriteLine("Conflict! Text from DB: {0}. Enter the corrected text:", textFromDB.NewsContent);
                Main();
            }
        }
Пример #9
0
        static void Main()
        {
            var context = new NewsDBContext();

            var data = context.News.Find(1);

            //WARNING: Concurency handling should be tested by running two instances of the app.

            while (true)
            {
                try
                {
                    data.Content = Console.ReadLine();
                    context.SaveChanges();
                    Console.WriteLine("Changes successfully saved in the DB.");
                    break;
                }
                catch (DbUpdateConcurrencyException exc)
                {
                    exc.Entries.Single().Reload();
                    Console.WriteLine("Conflict! Text from DB:" + context.News.Find(1).Content
                                      + ". Enter the corrected text:");
                }
            }
        }
Пример #10
0
        public static void Main()
        {
            using (var db = new NewsDBContext())
            {
                db.News.Add(new News { Content = "Some very long and interesting content!" });

                db.SaveChanges();
            }
        }
        public int Create(ECategories e)
        {
            int res = 0;

            try
            {
                DataAccess.Models.Categories obj = new Categories();
                obj.Name        = e.Name;
                obj.Description = e.Description;
                db.Categories.Add(obj);
                db.SaveChanges();
                res = obj.IdCategory;
            }
            catch (Exception)
            {
                throw;
            }
            return(res);
        }
Пример #12
0
        private static void CatchConcurrencyConflict(NewsDBContext context, New loadedEntity)
        {
            Console.WriteLine("Text from DB: {0}", loadedEntity.Content);
            Console.WriteLine("Enter the corrected text:");
            string newEntity = Console.ReadLine();

            loadedEntity.Content = newEntity;

            try
            {
                context.SaveChanges();
                Console.WriteLine("Changes successfully saved in the DB.");
            }
            catch (DbUpdateConcurrencyException)
            {
                Console.WriteLine("Conflict!");
                context.Entry(loadedEntity).Reload();
                CatchConcurrencyConflict(context, loadedEntity);
            }
        }
Пример #13
0
        /// <summary>
        /// Gets news and updates database records.
        /// </summary>
        /// <param name="url">target web URL.</param>
        /// <param name="type">target web category.</param>
        private void Update(string url, string type)
        {
            // Gets DbContext.
            var           builder    = new DbContextOptionsBuilder <NewsDBContext>();
            var           options    = builder.UseMySql(configuration.GetConnectionString("Default")).Options;
            NewsDBContext context    = new NewsDBContext(options);
            var           recordsSet = context.TblNews; // DbSet of DbContext.

            HtmlDocument htmlDoc = GetHtmlDoc(url);     // HTML Document.

            // Get TAG nodes.
            var nodes = htmlDoc.DocumentNode.SelectNodes(
                "//div[@class = 'tabContents active']/table/tr");

            // Get news contents.
            if (nodes != null)
            {
                int index = 0;
                foreach (var node in nodes)
                {
                    if (index == 0)
                    { // First node is table head, skip it.
                        index++;
                        continue;
                    }

                    var newsNode = node.Element("td").Element("a");    // News contents.

                    try
                    {                                       // If record is only one.
                        var record = recordsSet.Single(n => // Query a record that need update in set.
                                                       n.NewsRank == index && n.NewsType == type);

                        record.NewsUrl = newsNode.GetAttributeValue("href", null);    // URL

                        var doc     = GetHtmlDoc(record.NewsUrl);
                        var content = doc.DocumentNode.SelectSingleNode(
                            "//div[@class = 'post_content_main']");
                        var title = content.Element("h1").InnerText;
                        var text  = content.SelectSingleNode(
                            "//div[@class = 'post_body']/div[@class = 'post_text']")
                                    .SelectNodes("p");
                        string str = "";
                        foreach (var p in text)
                        {
                            str = str + "<p>" + p.InnerHtml + "</p>";
                        }

                        /* Get source.
                         * var source = content.SelectSingleNode("//div[@class = 'post_time_source']");
                         * source.RemoveChild(source.SelectSingleNode("//a[@class = 'post_jubao']"));
                         * Console.WriteLine(source.InnerText.Trim());
                         */

                        record.NewsTitle   = title;        // Title.
                        record.NewsContent = str;          // Content.
                        record.SubmitTime  = DateTime.Now; // Submit time.

                        context.SaveChanges();             // save update.
                    }
                    catch (Exception)
                    { // record is more or less than one.
                        break;
                    }

                    if (index == 10)
                    {
                        break;                 // Get only 10  pieces of news.
                    }
                    index++;
                } // foreach (var node in nodes) ---
            }     // if (nodes != null) ---
            else
            { // Getting nodes failed.
                Console.WriteLine("ERROR!");
            }
        } // private void Update(string url, string type) ---