From a93a0a33e8d548c7b016997cb57c3351c8c37702 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 8 Feb 2019 12:13:49 +0000 Subject: Added try catch in case table is empty --- Main/Program.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Main/Program.cs b/Main/Program.cs index 0159873..808de98 100644 --- a/Main/Program.cs +++ b/Main/Program.cs @@ -20,14 +20,29 @@ namespace Main { PublishDate = 4 } }; - string[] xmls = new [] { + string[] xmls = new[] { await Download.DownloadXML("http://feeds.bbci.co.uk/news/rss.xml") }; List
articles = ParserService.ParseMultiple(xmls, configs).ToList(); Console.WriteLine(articles.Count); using (var db = new DatabaseContext()) { - articles.ForEach(async article => await db.Articles.AddAsync(article)); + int start; + try { + start = db.Articles.Last().ArticleID; + } catch (Exception) { + start = 0; + } + var insertReady = articles.Select((article, index) => new Article() { + ArticleID = index + start, + Source = article.Source, + Title = article.Title, + Description = article.Description, + ArticleLink = article.ArticleLink, + ImageLink = article.ImageLink, + PublishDate = article.PublishDate + }).ToList(); + insertReady.ForEach(async article => await db.Articles.AddAsync(article)); int records = await db.SaveChangesAsync(); Console.WriteLine($"Wrote {records} records!"); } -- cgit v1.2.3-13-gbd6f