提交 fed32198 编写于 作者: 麦壳饼's avatar 麦壳饼

mysql is working .

上级 296fed88
......@@ -14,10 +14,12 @@ namespace Microsoft.Extensions.DependencyInjection
{
services.AddEntityFrameworkMySql();
services.AddSingleton<IDataBaseModelBuilderOptions>(c => new MySqlModelBuilderOptions());
var sv = ServerVersion.AutoDetect(connectionString);
services.AddSingleton(sv);
services.AddDbContextPool<ApplicationDbContext>(builder =>
{
builder.UseInternalServiceProvider(services.BuildServiceProvider());
builder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), s => s.MigrationsAssembly("IoTSharp.Data.MySQL"));
builder.UseMySql(connectionString, sv, s => s.MigrationsAssembly("IoTSharp.Data.MySQL"));
}
, poolSize );
......
......@@ -9,8 +9,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace IoTSharp.Data.MySQL.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20210214193940_MySQLFirstSchema")]
partial class MySQLFirstSchema
[Migration("20210215021817_MySqlFirstSchema")]
partial class MySqlFirstSchema
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
......@@ -26,16 +26,16 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("char(36)");
b.Property<string>("ActionData")
.HasColumnType("longtext CHARACTER SET utf8mb4");
.HasColumnType("JSON");
b.Property<string>("ActionName")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("ActionResult")
.HasColumnType("longtext CHARACTER SET utf8mb4");
.HasColumnType("JSON");
b.Property<DateTime>("ActiveDateTime")
.HasColumnType("datetime(6)");
.HasColumnType("timestamp");
b.Property<Guid?>("CustomerId")
.HasColumnType("char(36)");
......@@ -152,7 +152,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("int");
b.Property<DateTime>("DateTime")
.HasColumnType("timestamp with time zone(6)");
.HasColumnType("timestamp");
b.Property<int>("Type")
.HasColumnType("int");
......@@ -164,13 +164,13 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("tinyint(1)");
b.Property<DateTime>("Value_DateTime")
.HasColumnType("timestamp with time zone(6)");
.HasColumnType("timestamp");
b.Property<double>("Value_Double")
.HasColumnType("double");
b.Property<string>("Value_Json")
.HasColumnType("longtext CHARACTER SET utf8mb4");
.HasColumnType("JSON");
b.Property<long>("Value_Long")
.HasColumnType("bigint");
......@@ -208,7 +208,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("int");
b.Property<DateTime>("LastActive")
.HasColumnType("datetime(6)");
.HasColumnType("timestamp");
b.Property<string>("Name")
.HasColumnType("longtext CHARACTER SET utf8mb4");
......@@ -301,7 +301,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("varchar(255) CHARACTER SET utf8mb4");
b.Property<DateTime>("DateTime")
.HasColumnType("timestamp with time zone(6)");
.HasColumnType("timestamp");
b.Property<int>("DataSide")
.HasColumnType("int");
......@@ -316,13 +316,13 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("tinyint(1)");
b.Property<DateTime>("Value_DateTime")
.HasColumnType("timestamp with time zone(6)");
.HasColumnType("timestamp");
b.Property<double>("Value_Double")
.HasColumnType("double");
b.Property<string>("Value_Json")
.HasColumnType("longtext CHARACTER SET utf8mb4");
.HasColumnType("JSON");
b.Property<long>("Value_Long")
.HasColumnType("bigint");
......
......@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace IoTSharp.Data.MySQL.Migrations
{
public partial class MySQLFirstSchema : Migration
public partial class MySqlFirstSchema : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
......@@ -54,15 +54,15 @@ namespace IoTSharp.Data.MySQL.Migrations
Catalog = table.Column<int>(type: "int", nullable: false),
DeviceId = table.Column<Guid>(type: "char(36)", nullable: false),
KeyName = table.Column<string>(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false),
DateTime = table.Column<DateTime>(type: "timestamp with time zone(6)", nullable: false),
DateTime = table.Column<DateTime>(type: "timestamp", nullable: false),
DataSide = table.Column<int>(type: "int", nullable: false),
Type = table.Column<int>(type: "int", nullable: false),
Value_Boolean = table.Column<bool>(type: "tinyint(1)", nullable: false),
Value_String = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_Long = table.Column<long>(type: "bigint", nullable: false),
Value_DateTime = table.Column<DateTime>(type: "timestamp with time zone(6)", nullable: false),
Value_DateTime = table.Column<DateTime>(type: "timestamp", nullable: false),
Value_Double = table.Column<double>(type: "double", nullable: false),
Value_Json = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_Json = table.Column<string>(type: "JSON", nullable: true),
Value_XML = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_Binary = table.Column<byte[]>(type: "longblob", nullable: true)
},
......@@ -75,7 +75,7 @@ namespace IoTSharp.Data.MySQL.Migrations
name: "TelemetryData",
columns: table => new
{
DateTime = table.Column<DateTime>(type: "timestamp with time zone(6)", nullable: false),
DateTime = table.Column<DateTime>(type: "timestamp", nullable: false),
DeviceId = table.Column<Guid>(type: "char(36)", nullable: false),
KeyName = table.Column<string>(type: "varchar(255) CHARACTER SET utf8mb4", nullable: false),
DataSide = table.Column<int>(type: "int", nullable: false),
......@@ -83,9 +83,9 @@ namespace IoTSharp.Data.MySQL.Migrations
Value_Boolean = table.Column<bool>(type: "tinyint(1)", nullable: false),
Value_String = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_Long = table.Column<long>(type: "bigint", nullable: false),
Value_DateTime = table.Column<DateTime>(type: "timestamp with time zone(6)", nullable: false),
Value_DateTime = table.Column<DateTime>(type: "timestamp", nullable: false),
Value_Double = table.Column<double>(type: "double", nullable: false),
Value_Json = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_Json = table.Column<string>(type: "JSON", nullable: true),
Value_XML = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_Binary = table.Column<byte[]>(type: "longblob", nullable: true)
},
......@@ -260,9 +260,9 @@ namespace IoTSharp.Data.MySQL.Migrations
ObjectName = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
ObjectType = table.Column<int>(type: "int", nullable: false),
ActionName = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
ActionData = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
ActionResult = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
ActiveDateTime = table.Column<DateTime>(type: "datetime(6)", nullable: false)
ActionData = table.Column<string>(type: "JSON", nullable: true),
ActionResult = table.Column<string>(type: "JSON", nullable: true),
ActiveDateTime = table.Column<DateTime>(type: "timestamp", nullable: false)
},
constraints: table =>
{
......@@ -348,7 +348,7 @@ namespace IoTSharp.Data.MySQL.Migrations
Name = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
DeviceType = table.Column<int>(type: "int", nullable: false),
Online = table.Column<bool>(type: "tinyint(1)", nullable: false),
LastActive = table.Column<DateTime>(type: "datetime(6)", nullable: false),
LastActive = table.Column<DateTime>(type: "timestamp", nullable: false),
Timeout = table.Column<int>(type: "int", nullable: false),
OwnerId = table.Column<Guid>(type: "char(36)", nullable: true),
TenantId = table.Column<Guid>(type: "char(36)", nullable: true),
......
......@@ -24,16 +24,16 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("char(36)");
b.Property<string>("ActionData")
.HasColumnType("longtext CHARACTER SET utf8mb4");
.HasColumnType("JSON");
b.Property<string>("ActionName")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("ActionResult")
.HasColumnType("longtext CHARACTER SET utf8mb4");
.HasColumnType("JSON");
b.Property<DateTime>("ActiveDateTime")
.HasColumnType("datetime(6)");
.HasColumnType("timestamp");
b.Property<Guid?>("CustomerId")
.HasColumnType("char(36)");
......@@ -150,7 +150,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("int");
b.Property<DateTime>("DateTime")
.HasColumnType("timestamp with time zone(6)");
.HasColumnType("timestamp");
b.Property<int>("Type")
.HasColumnType("int");
......@@ -162,13 +162,13 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("tinyint(1)");
b.Property<DateTime>("Value_DateTime")
.HasColumnType("timestamp with time zone(6)");
.HasColumnType("timestamp");
b.Property<double>("Value_Double")
.HasColumnType("double");
b.Property<string>("Value_Json")
.HasColumnType("longtext CHARACTER SET utf8mb4");
.HasColumnType("JSON");
b.Property<long>("Value_Long")
.HasColumnType("bigint");
......@@ -206,7 +206,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("int");
b.Property<DateTime>("LastActive")
.HasColumnType("datetime(6)");
.HasColumnType("timestamp");
b.Property<string>("Name")
.HasColumnType("longtext CHARACTER SET utf8mb4");
......@@ -299,7 +299,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("varchar(255) CHARACTER SET utf8mb4");
b.Property<DateTime>("DateTime")
.HasColumnType("timestamp with time zone(6)");
.HasColumnType("timestamp");
b.Property<int>("DataSide")
.HasColumnType("int");
......@@ -314,13 +314,13 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("tinyint(1)");
b.Property<DateTime>("Value_DateTime")
.HasColumnType("timestamp with time zone(6)");
.HasColumnType("timestamp");
b.Property<double>("Value_Double")
.HasColumnType("double");
b.Property<string>("Value_Json")
.HasColumnType("longtext CHARACTER SET utf8mb4");
.HasColumnType("JSON");
b.Property<long>("Value_Long")
.HasColumnType("bigint");
......
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -13,40 +14,66 @@ namespace IoTSharp.Data.MySQL
{
}
public IInfrastructure<IServiceProvider> Infrastructure { get; set; }
public void OnModelCreating(ModelBuilder modelBuilder)
{
//modelBuilder.Entity<TelemetryData>()
//.Property(b => b.Value_Json)
//.HasColumnType("jsonb");
var sv = Infrastructure.GetService<ServerVersion>();
modelBuilder.Entity<TelemetryData>()
.Property(b => b.DateTime)
.HasColumnType("timestamp");
modelBuilder.Entity<TelemetryData>()
.Property(b => b.Value_DateTime)
.HasColumnType("timestamp");
//modelBuilder.Entity<TelemetryData>()
//.Property(b => b.Value_XML)
//.HasColumnType("xml");
//modelBuilder.Entity<AttributeLatest>()
//.Property(b => b.Value_Json)
//.HasColumnType("jsonb");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.DateTime)
.HasColumnType("timestamp");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.Value_DateTime)
.HasColumnType("timestamp");
//modelBuilder.Entity<AttributeLatest>()
//.Property(b => b.Value_XML)
//.HasColumnType("xml");
modelBuilder.Entity<AttributeLatest>()
.Property(b => b.DateTime)
.HasColumnType("timestamp");
modelBuilder.Entity<AttributeLatest>()
.Property(b => b.Value_DateTime)
.HasColumnType("timestamp");
modelBuilder.Entity<AuditLog>()
.Property(b => b.ActiveDateTime)
.HasColumnType("timestamp");
modelBuilder.Entity<Device>()
.Property(b => b.LastActive)
.HasColumnType("timestamp");
if (sv.Supports.Json)
{
modelBuilder.Entity<TelemetryData>()
.Property(b => b.Value_Json)
.HasColumnType("JSON");
modelBuilder.Entity<TelemetryData>()
.Property(b => b.Value_Json)
.HasColumnType("JSON");
//modelBuilder.Entity<TelemetryLatest>()
//.Property(b => b.Value_Json)
//.HasColumnType("jsonb");
modelBuilder.Entity<AttributeLatest>()
.Property(b => b.Value_Json)
.HasColumnType("JSON");
//modelBuilder.Entity<TelemetryLatest>()
//.Property(b => b.Value_XML)
//.HasColumnType("xml");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.Value_Json)
.HasColumnType("JSON");
//modelBuilder.Entity<AuditLog>()
//.Property(b => b.ActionData)
//.HasColumnType("jsonb");
modelBuilder.Entity<AuditLog>()
.Property(b => b.ActionData)
.HasColumnType("JSON");
//modelBuilder.Entity<AuditLog>()
//.Property(b => b.ActionResult)
//.HasColumnType("jsonb");
modelBuilder.Entity<AuditLog>()
.Property(b => b.ActionResult)
.HasColumnType("JSON");
}
}
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IoTSharp.Data.PostgreSQL
{
......@@ -11,10 +8,34 @@ namespace IoTSharp.Data.PostgreSQL
{
public NpgsqlModelBuilderOptions()
{
}
public IInfrastructure<IServiceProvider> Infrastructure { get; set; }
public void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TelemetryData>()
.Property(b => b.DateTime)
.HasColumnType("timestamp with time zone");
modelBuilder.Entity<TelemetryData>()
.Property(b => b.Value_DateTime)
.HasColumnType("timestamp with time zone");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.DateTime)
.HasColumnType("timestamp with time zone");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.Value_DateTime)
.HasColumnType("timestamp with time zone");
modelBuilder.Entity<AttributeLatest>()
.Property(b => b.DateTime)
.HasColumnType("timestamp with time zone");
modelBuilder.Entity<AttributeLatest>()
.Property(b => b.Value_DateTime)
.HasColumnType("timestamp with time zone");
modelBuilder.Entity<TelemetryData>()
.Property(b => b.Value_Json)
.HasColumnType("jsonb");
......@@ -31,7 +52,6 @@ namespace IoTSharp.Data.PostgreSQL
.Property(b => b.Value_XML)
.HasColumnType("xml");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.Value_Json)
.HasColumnType("jsonb");
......@@ -49,4 +69,4 @@ namespace IoTSharp.Data.PostgreSQL
.HasColumnType("jsonb");
}
}
}
}
\ No newline at end of file
......@@ -16,7 +16,6 @@ namespace IoTSharp.Data
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
if (Database.GetPendingMigrations().Count() > 0)
{
Database.Migrate();
......@@ -42,7 +41,9 @@ namespace IoTSharp.Data
modelBuilder.Entity<TelemetryLatest>().HasDiscriminator<DataCatalog>(nameof(Data.DataStorage.Catalog));
modelBuilder.Entity<Device>().HasDiscriminator<DeviceType>(nameof(Data.Device.DeviceType)).HasValue<Gateway>(DeviceType.Gateway).HasValue<Device>(DeviceType.Device);
modelBuilder.Entity<Gateway>().HasDiscriminator<DeviceType>(nameof(Data.Device.DeviceType));
this.GetService<IDataBaseModelBuilderOptions>().OnModelCreating(modelBuilder);
var builder_options= this.GetService<IDataBaseModelBuilderOptions>();
builder_options.Infrastructure = this;
builder_options.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new TelemetryDataConfiguration());
base.OnModelCreating(modelBuilder);
}
......
......@@ -36,7 +36,7 @@ namespace IoTSharp.Data
[Column(Order = 2)]
public string KeyName { get; set; }
[Column(Order = 3,TypeName = "timestamp with time zone")]
[Column(Order = 3 )]
public DateTime DateTime { get; set; }
[Column(Order = 4)]
......@@ -50,7 +50,7 @@ namespace IoTSharp.Data
public bool Value_Boolean { get; set; }
public string Value_String { get; set; }
public long Value_Long { get; set; }
[Column(TypeName = "timestamp with time zone")]
public DateTime Value_DateTime { get; set; }
public double Value_Double { get; set; }
public string Value_Json { get; set; }
......
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -9,6 +10,7 @@ namespace IoTSharp.Data
{
public interface IDataBaseModelBuilderOptions
{
public IInfrastructure<IServiceProvider> Infrastructure { get; set; }
public void OnModelCreating(ModelBuilder modelBuilder);
}
}
......@@ -16,7 +16,7 @@ namespace IoTSharp.Data
[Column(Order = 2)]
public string KeyName { get; set; }
[Column(Order = 3, TypeName = "timestamp with time zone")]
[Column(Order = 3)]
public DateTime DateTime { get; set; }
[Column(Order = 4)]
......@@ -30,7 +30,7 @@ namespace IoTSharp.Data
public bool Value_Boolean { get; set; }
public string Value_String { get; set; }
public long Value_Long { get; set; }
[Column(TypeName = "timestamp with time zone")]
public DateTime Value_DateTime { get; set; }
public double Value_Double { get; set; }
public string Value_Json { get; set; }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册