ApplicationDbContext.cs 4.9 KB
Newer Older
1 2
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
麦壳饼's avatar
麦壳饼 已提交
3 4 5 6
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Linq;
麦壳饼's avatar
麦壳饼 已提交
7

麦壳饼's avatar
麦壳饼 已提交
8 9 10 11
namespace IoTSharp.Hub.Data
{
    public class ApplicationDbContext : IdentityDbContext
    {
麦壳饼's avatar
麦壳饼 已提交
12 13
        private IConfiguration _configuration;

麦壳饼's avatar
麦壳饼 已提交
14 15 16 17 18 19 20 21 22
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IConfiguration configuration)
            : base(options)
        {
            _configuration = configuration;
            var _DataBase = configuration["DataBase"] ?? "sqlite";
            if (Enum.TryParse(_DataBase, out DatabaseType databaseType))
            {
                DatabaseType = databaseType;
            }
麦壳饼's avatar
麦壳饼 已提交
23
            if (Database.GetPendingMigrations().Count() > 0)
麦壳饼's avatar
麦壳饼 已提交
24
            {
麦壳饼's avatar
麦壳饼 已提交
25
                Database.Migrate();
麦壳饼's avatar
麦壳饼 已提交
26 27
            }
        }
麦壳饼's avatar
麦壳饼 已提交
28

麦壳饼's avatar
麦壳饼 已提交
29 30 31 32
        public DatabaseType DatabaseType { get; private set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
麦壳饼's avatar
麦壳饼 已提交
33
            base.OnModelCreating(modelBuilder);
麦壳饼's avatar
麦壳饼 已提交
34

麦壳饼's avatar
麦壳饼 已提交
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
            modelBuilder.Entity<DataStorage>()
           .HasDiscriminator<DataCatalog>(nameof(Data.DataStorage.Catalog))
           .HasValue<DataStorage>(DataCatalog.None)
           .HasValue<AttributeData>(DataCatalog.AttributeData)
                  .HasValue<AttributeLatest>(DataCatalog.AttributeLatest)
                         .HasValue<TelemetryData>(DataCatalog.TelemetryData)
           .HasValue<TelemetryLatest>(DataCatalog.TelemetryLatest);

            modelBuilder.Entity<AttributeData>().HasDiscriminator<DataCatalog>(nameof(Data.DataStorage.Catalog));
            modelBuilder.Entity<AttributeLatest>().HasDiscriminator<DataCatalog>(nameof(Data.DataStorage.Catalog));
            modelBuilder.Entity<TelemetryData>().HasDiscriminator<DataCatalog>(nameof(Data.DataStorage.Catalog));
            modelBuilder.Entity<TelemetryLatest>().HasDiscriminator<DataCatalog>(nameof(Data.DataStorage.Catalog));

            switch (DatabaseType)
            {
                case DatabaseType.mssql:
                    ForSqlServer(modelBuilder);
                    break;

                case DatabaseType.npgsql:
                    ForNpgsql(modelBuilder);
                    break;

                case DatabaseType.sqlite:
                    break;

                default:
                    break;
            }
麦壳饼's avatar
麦壳饼 已提交
64 65 66 67
        }

        private void ForNpgsql(ModelBuilder modelBuilder)
        {
麦壳饼's avatar
麦壳饼 已提交
68
            modelBuilder.Entity<TelemetryData>()
麦壳饼's avatar
麦壳饼 已提交
69 70 71
            .Property(b => b.Value_Json)
            .HasColumnType("jsonb");

麦壳饼's avatar
麦壳饼 已提交
72
            modelBuilder.Entity<TelemetryData>()
麦壳饼's avatar
麦壳饼 已提交
73 74 75
            .Property(b => b.Value_XML)
            .HasColumnType("xml");

麦壳饼's avatar
麦壳饼 已提交
76
            modelBuilder.Entity<AttributeLatest>()
麦壳饼's avatar
麦壳饼 已提交
77 78 79
            .Property(b => b.Value_Json)
            .HasColumnType("jsonb");

麦壳饼's avatar
麦壳饼 已提交
80
            modelBuilder.Entity<AttributeLatest>()
麦壳饼's avatar
麦壳饼 已提交
81 82 83
            .Property(b => b.Value_XML)
            .HasColumnType("xml");

84
            modelBuilder.Entity<AttributeData>()
麦壳饼's avatar
麦壳饼 已提交
85 86 87
            .Property(b => b.Value_Json)
            .HasColumnType("jsonb");

88
            modelBuilder.Entity<AttributeData>()
麦壳饼's avatar
麦壳饼 已提交
89 90 91 92
            .Property(b => b.Value_XML)
            .HasColumnType("xml");

            modelBuilder.Entity<TelemetryLatest>()
麦壳饼's avatar
麦壳饼 已提交
93 94
            .Property(b => b.Value_Json)
            .HasColumnType("jsonb");
麦壳饼's avatar
麦壳饼 已提交
95 96

            modelBuilder.Entity<TelemetryLatest>()
麦壳饼's avatar
麦壳饼 已提交
97 98
            .Property(b => b.Value_XML)
            .HasColumnType("xml");
麦壳饼's avatar
麦壳饼 已提交
99 100 101 102 103 104 105 106 107 108

            modelBuilder.Entity<AuditLog>()
            .Property(b => b.ActionData)
            .HasColumnType("jsonb");

            modelBuilder.Entity<AuditLog>()
            .Property(b => b.ActionResult)
            .HasColumnType("jsonb");

 
麦壳饼's avatar
麦壳饼 已提交
109
        }
麦壳饼's avatar
麦壳饼 已提交
110

麦壳饼's avatar
麦壳饼 已提交
111 112
        private void ForSqlServer(ModelBuilder modelBuilder)
        {
麦壳饼's avatar
麦壳饼 已提交
113
            modelBuilder.Entity<TelemetryData>()
麦壳饼's avatar
麦壳饼 已提交
114 115
            .Property(b => b.Value_XML)
            .HasColumnType("xml");
麦壳饼's avatar
麦壳饼 已提交
116

麦壳饼's avatar
麦壳饼 已提交
117
            modelBuilder.Entity<AttributeLatest>()
麦壳饼's avatar
麦壳饼 已提交
118 119
            .Property(b => b.Value_XML)
            .HasColumnType("xml");
麦壳饼's avatar
麦壳饼 已提交
120

麦壳饼's avatar
麦壳饼 已提交
121
            modelBuilder.Entity<DataStorage>()
麦壳饼's avatar
麦壳饼 已提交
122 123
            .Property(b => b.Value_XML)
            .HasColumnType("xml");
麦壳饼's avatar
麦壳饼 已提交
124 125

            modelBuilder.Entity<TelemetryLatest>()
麦壳饼's avatar
麦壳饼 已提交
126 127
            .Property(b => b.Value_XML)
            .HasColumnType("xml");
麦壳饼's avatar
麦壳饼 已提交
128
        }
麦壳饼's avatar
麦壳饼 已提交
129

麦壳饼's avatar
麦壳饼 已提交
130 131 132 133 134 135 136 137 138
        public DbSet<Tenant> Tenant { get; set; }
        public DbSet<Customer> Customer { get; set; }
        public DbSet<Relationship> Relationship { get; set; }
        public DbSet<Device> Device { get; set; }
        public DbSet<TelemetryData> TelemetryData { get; set; }
        public DbSet<AttributeLatest> AttributeLatest { get; set; }
        public DbSet<DataStorage> DataStorage { get; set; }
        public DbSet<AttributeData> AttributeData { get; set; }
        public DbSet<TelemetryLatest> TelemetryLatest { get; set; }
麦壳饼's avatar
麦壳饼 已提交
139
        public DbSet<DeviceIdentity> DeviceIdentities { get; set; }
麦壳饼's avatar
麦壳饼 已提交
140
        public DbSet<AuditLog> AuditLog { get; set; }
麦壳饼's avatar
麦壳饼 已提交
141 142
    }
}