aboutsummaryrefslogtreecommitdiff
path: root/Database
diff options
context:
space:
mode:
authorAryadev Chavali <aryadevchavali1@gmail.com>2019-02-10 15:12:39 +0000
committerAryadev Chavali <aryadevchavali1@gmail.com>2019-02-10 15:12:39 +0000
commit10877db46cb4c5efa0448ae3218f948bae16303c (patch)
treee07a6de8568b1ea7cebc1a185c020dd43c7b02ae /Database
parent9ed58b9d5d3babef95be122d12eda3239c43a738 (diff)
downloadnewsaggregator-10877db46cb4c5efa0448ae3218f948bae16303c.tar.gz
newsaggregator-10877db46cb4c5efa0448ae3218f948bae16303c.tar.bz2
newsaggregator-10877db46cb4c5efa0448ae3218f948bae16303c.zip
Made function using results from test to insert multiple articles into table, without duplicates
Diffstat (limited to 'Database')
-rw-r--r--Database/Database.cs10
1 files changed, 10 insertions, 0 deletions
diff --git a/Database/Database.cs b/Database/Database.cs
index 75c383f..14569aa 100644
--- a/Database/Database.cs
+++ b/Database/Database.cs
@@ -1,4 +1,5 @@
using System;
+using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
@@ -22,6 +23,15 @@ namespace Database {
public virtual DbSet<Article> Articles { get; set; }
public virtual DbSet<Config> Configs { get; set; }
+ public async void InsertArticles(IEnumerable<Article> articles) {
+ try {
+ await Articles.AddRangeAsync(articles);
+ await SaveChangesAsync();
+ } catch (Microsoft.EntityFrameworkCore.DbUpdateException) {
+ return; // duplicate title id's
+ }
+ }
+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
if (!optionsBuilder.IsConfigured) {
optionsBuilder.UseMySQL($"server=localhost;port=3306;user={Details["username"]};password={Details["password"]};database={Details["database"]}");