public async Task <int> AddArticlesToArticlesTableAsync(Article articleToAdd) { var context = new KnowledgeHubDataBaseContext(); //context.ChangeTracker.AutoDetectChangesEnabled = false; try { Article articleExists = context.Article.FirstOrDefault(article => article.Url == articleToAdd.Url); if (articleExists == null) { Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry <Article> article = context.Article.Add(articleToAdd); await context.SaveChangesAsync(); return(article.Entity.Id); } return(articleExists.Id); } catch (Exception ex) { var fallback = context.Article.FirstOrDefault(article => article.Url == articleToAdd.Url); if (fallback != null) { return(fallback.Id); } return(0); } }
public async Task AddArticlesToUserTableAsync(UserArticle userArticle, int tagId, int userId) { var context = new KnowledgeHubDataBaseContext(); //context.ChangeTracker.AutoDetectChangesEnabled = false; try { userArticle.TagId = context.UserTag.FirstOrDefault(t => t.UserId == userId && t.TagId == tagId).Id; context.UserArticle.Add(userArticle); await context.SaveChangesAsync(); } catch (Exception ex) { return; } }