blob: d3ccb2ec26a8f2b500acdb101c777c45e868021a (
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Classes;
namespace Database {
public partial class DatabaseContext : DbContext {
private Dictionary<string, string> Details;
public DatabaseContext() {
Details = Task.Run(async () => await DetailsHelper.GetDetails()).GetAwaiter().GetResult();
}
public DatabaseContext(DbContextOptions<DatabaseContext> options)
: base(options) {
Details = Task.Run(async () => await DetailsHelper.GetDetails()).GetAwaiter().GetResult();
}
public virtual DbSet<Article> Articles { get; set; }
public virtual DbSet<Config> Configs { get; set; }
public async Task<int> InsertArticles(IEnumerable<Article> articles) {
try {
await Articles.AddRangeAsync(articles);
return await SaveChangesAsync();
} catch (Microsoft.EntityFrameworkCore.DbUpdateException) {
return -1; // 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"]}");
}
}
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(64)
.IsUnicode(false);
entity.Property(e => e.Description)
.HasColumnName("description")
.HasMaxLength(255)
.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);
});
modelBuilder.Entity<Config>(entity => {
entity.HasKey(e => e.ConfigID);
entity.ToTable("configs", "news_aggregator");
entity.Property(e => e.ConfigID)
.HasColumnName("config_id")
.HasColumnType("int(11)")
.ValueGeneratedNever();
entity.Property(e => e.ArticleLink)
.HasColumnName("article_link")
.HasColumnType("int(11)");
entity.Property(e => e.Description)
.HasColumnName("description")
.HasColumnType("int(11)");
entity.Property(e => e.ImageLink)
.HasColumnName("image_link")
.HasColumnType("int(11)");
entity.Property(e => e.PublishDate)
.HasColumnName("publish_date")
.HasColumnType("int(11)");
entity.Property(e => e.Source)
.HasMaxLength(20)
.IsUnicode(false);
entity.Property(e => e.Title)
.HasColumnName("title")
.HasColumnType("int(11)");
});
}
}
}
|