From 10877db46cb4c5efa0448ae3218f948bae16303c Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 10 Feb 2019 15:12:39 +0000 Subject: Made function using results from test to insert multiple articles into table, without duplicates --- Database/Database.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) 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
Articles { get; set; } public virtual DbSet Configs { get; set; } + public async void InsertArticles(IEnumerable
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"]}"); -- cgit v1.2.3-13-gbd6f