-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cs
39 lines (32 loc) · 1.09 KB
/
Database.cs
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
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
namespace Mqtt2Sql
{
public class MqttMessage
{
public virtual string Topic { get; set; }
public virtual DateTime Timestamp { get; set; }
public virtual string Payload { get; set; }
}
/// <summary>
/// This is NOT how to do databases!
/// </summary>
public class Database : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
IConfiguration cfg = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile("appsettings.json", false)
.Build();
optionsBuilder.UseSqlServer(cfg.GetConnectionString("Mqtt"));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MqttMessage>().HasKey(z => new { z.Topic, z.Timestamp });
}
public virtual DbSet<MqttMessage> Messages { get; set; }
}
}