aboutsummaryrefslogtreecommitdiff
path: root/Database/Database.cs
blob: 04c9619491b1d4537324d1f8666cc9c32a225ea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Classes;

namespace Database {
    /// <summary>
    /// Database context for MySQL
    /// </summary>
    public partial class DatabaseContext : DbContext {
        public DatabaseContext() { }

        public DatabaseContext(DbContextOptions<DatabaseContext> options)
            : base(options) { }

        public virtual DbSet<Article> Articles { get; set; }

        /// <summary>
        /// Insert multiple articles into database
        /// </summary>
        /// <param name="articles">articles to insert</param>
        public async Task InsertArticles(IEnumerable<Article> articles) {
            foreach (Article article in articles) {
                try {
                    await Articles.AddAsync(article);
                    await SaveChangesAsync();
                }
                catch (Microsoft.EntityFrameworkCore.DbUpdateException) {
                    continue;
                }
            }
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { }

        /// <summary>
        /// Model for MySQL database to entity
        /// </summary>
        protected override void OnModelCreating(ModelBuilder modelBuilder) {
            modelBuilder.HasAnnotation("ProductVersion", "2.2.1-servicing-10028");

            modelBuilder.Entity<Article>(entity => {
                entity.HasKey(e => e.ArticleID);

                entity.ToTable("articles", "news_aggregator");

                entity.Property(e => e.ArticleID)
                    .HasColumnName("article_id")
                    .HasColumnType("int(11)")
                    .ValueGeneratedOnAdd();

                entity.Property(e => e.ArticleLink)
                    .HasColumnName("article_link")
                    .HasMaxLength(128)
                    .IsUnicode(false);

                entity.Property(e => e.Description)
                    .HasColumnName("description")
                    .HasMaxLength(512)
                    .IsUnicode(false);

                entity.Property(e => e.ImageLink)
                    .HasColumnName("image_link")
                    .HasMaxLength(128)
                    .IsUnicode(false);

                entity.Property(e => e.PublishDate)
                    .HasColumnName("publish_date")
                    .HasColumnType("date")
                    .ValueGeneratedNever();

                entity.Property(e => e.Source)
                    .HasColumnName("source")
                    .HasMaxLength(8)
                    .IsUnicode(false);

                entity.Property(e => e.Title)
                    .HasColumnName("title")
                    .HasMaxLength(255)
                    .IsUnicode(false);
            });
        }
    }
}