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

modify fo mysql

上级 0fecf0ab
......@@ -20,4 +20,8 @@
<ProjectReference Include="..\IoTSharp.Data\IoTSharp.Data.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project>

using EFCore.Sharding;
using IoTSharp.Data;
using IoTSharp.Data.MySQL;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
......@@ -11,8 +12,11 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static void ConfigureMySql(this IServiceCollection services, string connectionString,int poolSize , IHealthChecksBuilder checksBuilder, HealthChecksUIBuilder healthChecksUI)
{
services.AddEntityFrameworkMySql();
services.AddSingleton<IDataBaseModelBuilderOptions>(c => new MySqlModelBuilderOptions());
services.AddDbContextPool<ApplicationDbContext>(builder =>
{
builder.UseInternalServiceProvider(services.BuildServiceProvider());
builder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), s => s.MigrationsAssembly("IoTSharp.Data.MySQL"));
}
, poolSize );
......
......@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace IoTSharp.Data.MySQL.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20210214185821_MySQLFirstSchema")]
[Migration("20210214193940_MySQLFirstSchema")]
partial class MySQLFirstSchema
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
......@@ -26,13 +26,13 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("char(36)");
b.Property<string>("ActionData")
.HasColumnType("jsonb");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("ActionName")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("ActionResult")
.HasColumnType("jsonb");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<DateTime>("ActiveDateTime")
.HasColumnType("datetime(6)");
......@@ -170,7 +170,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("double");
b.Property<string>("Value_Json")
.HasColumnType("jsonb");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<long>("Value_Long")
.HasColumnType("bigint");
......@@ -179,7 +179,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("Value_XML")
.HasColumnType("xml");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("Catalog", "DeviceId", "KeyName");
......@@ -322,7 +322,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("double");
b.Property<string>("Value_Json")
.HasColumnType("jsonb");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<long>("Value_Long")
.HasColumnType("bigint");
......@@ -331,7 +331,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("Value_XML")
.HasColumnType("xml");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("DeviceId", "KeyName", "DateTime");
......
......@@ -62,8 +62,8 @@ namespace IoTSharp.Data.MySQL.Migrations
Value_Long = table.Column<long>(type: "bigint", nullable: false),
Value_DateTime = table.Column<DateTime>(type: "timestamp with time zone(6)", nullable: false),
Value_Double = table.Column<double>(type: "double", nullable: false),
Value_Json = table.Column<string>(type: "jsonb", nullable: true),
Value_XML = table.Column<string>(type: "xml", nullable: true),
Value_Json = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_XML = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_Binary = table.Column<byte[]>(type: "longblob", nullable: true)
},
constraints: table =>
......@@ -85,8 +85,8 @@ namespace IoTSharp.Data.MySQL.Migrations
Value_Long = table.Column<long>(type: "bigint", nullable: false),
Value_DateTime = table.Column<DateTime>(type: "timestamp with time zone(6)", nullable: false),
Value_Double = table.Column<double>(type: "double", nullable: false),
Value_Json = table.Column<string>(type: "jsonb", nullable: true),
Value_XML = table.Column<string>(type: "xml", nullable: true),
Value_Json = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_XML = table.Column<string>(type: "longtext CHARACTER SET utf8mb4", nullable: true),
Value_Binary = table.Column<byte[]>(type: "longblob", nullable: true)
},
constraints: table =>
......@@ -260,8 +260,8 @@ 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: "jsonb", nullable: true),
ActionResult = table.Column<string>(type: "jsonb", 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)
},
constraints: table =>
......
......@@ -24,13 +24,13 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("char(36)");
b.Property<string>("ActionData")
.HasColumnType("jsonb");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("ActionName")
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("ActionResult")
.HasColumnType("jsonb");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<DateTime>("ActiveDateTime")
.HasColumnType("datetime(6)");
......@@ -168,7 +168,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("double");
b.Property<string>("Value_Json")
.HasColumnType("jsonb");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<long>("Value_Long")
.HasColumnType("bigint");
......@@ -177,7 +177,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("Value_XML")
.HasColumnType("xml");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("Catalog", "DeviceId", "KeyName");
......@@ -320,7 +320,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("double");
b.Property<string>("Value_Json")
.HasColumnType("jsonb");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<long>("Value_Long")
.HasColumnType("bigint");
......@@ -329,7 +329,7 @@ namespace IoTSharp.Data.MySQL.Migrations
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.Property<string>("Value_XML")
.HasColumnType("xml");
.HasColumnType("longtext CHARACTER SET utf8mb4");
b.HasKey("DeviceId", "KeyName", "DateTime");
......
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IoTSharp.Data.MySQL
{
public class MySqlModelBuilderOptions : IDataBaseModelBuilderOptions
{
public MySqlModelBuilderOptions()
{
}
public void OnModelCreating(ModelBuilder modelBuilder)
{
//modelBuilder.Entity<TelemetryData>()
//.Property(b => b.Value_Json)
//.HasColumnType("jsonb");
//modelBuilder.Entity<TelemetryData>()
//.Property(b => b.Value_XML)
//.HasColumnType("xml");
//modelBuilder.Entity<AttributeLatest>()
//.Property(b => b.Value_Json)
//.HasColumnType("jsonb");
//modelBuilder.Entity<AttributeLatest>()
//.Property(b => b.Value_XML)
//.HasColumnType("xml");
//modelBuilder.Entity<TelemetryLatest>()
//.Property(b => b.Value_Json)
//.HasColumnType("jsonb");
//modelBuilder.Entity<TelemetryLatest>()
//.Property(b => b.Value_XML)
//.HasColumnType("xml");
//modelBuilder.Entity<AuditLog>()
//.Property(b => b.ActionData)
//.HasColumnType("jsonb");
//modelBuilder.Entity<AuditLog>()
//.Property(b => b.ActionResult)
//.HasColumnType("jsonb");
}
}
}
......@@ -8,6 +8,7 @@
<PackageReference Include="AspNetCore.HealthChecks.NpgSql" Version="5.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI.PostgreSQL.Storage" Version="5.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Core" Version="5.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
......

using EFCore.Sharding;
using IoTSharp.Data;
using IoTSharp.Data.PostgreSQL;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
......@@ -9,11 +10,16 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class IoTSharpDataBuilderExtensions
{
public static void ConfigureNpgsql(this IServiceCollection services, string connectionString, int poolSize, IHealthChecksBuilder checksBuilder, HealthChecksUIBuilder healthChecksUI)
{
services.AddEntityFrameworkNpgsql();
services.AddSingleton<IDataBaseModelBuilderOptions>( c=> new NpgsqlModelBuilderOptions());
services.AddDbContextPool<ApplicationDbContext>(builder =>
{
builder.UseNpgsql(connectionString, s => s.MigrationsAssembly("IoTSharp.Data.PostgreSQL"));
builder.UseNpgsql(connectionString, s => s.MigrationsAssembly("IoTSharp.Data.PostgreSQL"));
builder.UseInternalServiceProvider(services.BuildServiceProvider());
}
, poolSize);
checksBuilder.AddNpgSql(connectionString);
......
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IoTSharp.Data.PostgreSQL
{
public class NpgsqlModelBuilderOptions : IDataBaseModelBuilderOptions
{
public NpgsqlModelBuilderOptions()
{
}
public void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TelemetryData>()
.Property(b => b.Value_Json)
.HasColumnType("jsonb");
modelBuilder.Entity<TelemetryData>()
.Property(b => b.Value_XML)
.HasColumnType("xml");
modelBuilder.Entity<AttributeLatest>()
.Property(b => b.Value_Json)
.HasColumnType("jsonb");
modelBuilder.Entity<AttributeLatest>()
.Property(b => b.Value_XML)
.HasColumnType("xml");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.Value_Json)
.HasColumnType("jsonb");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.Value_XML)
.HasColumnType("xml");
modelBuilder.Entity<AuditLog>()
.Property(b => b.ActionData)
.HasColumnType("jsonb");
modelBuilder.Entity<AuditLog>()
.Property(b => b.ActionResult)
.HasColumnType("jsonb");
}
}
}
......@@ -4,15 +4,19 @@ using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
namespace IoTSharp.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
if (Database.GetPendingMigrations().Count() > 0)
{
Database.Migrate();
......@@ -38,47 +42,13 @@ 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));
ForNpgsql(modelBuilder);
this.GetService<IDataBaseModelBuilderOptions>().OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new TelemetryDataConfiguration());
base.OnModelCreating(modelBuilder);
}
private void ForNpgsql(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TelemetryData>()
.Property(b => b.Value_Json)
.HasColumnType("jsonb");
modelBuilder.Entity<TelemetryData>()
.Property(b => b.Value_XML)
.HasColumnType("xml");
modelBuilder.Entity<AttributeLatest>()
.Property(b => b.Value_Json)
.HasColumnType("jsonb");
modelBuilder.Entity<AttributeLatest>()
.Property(b => b.Value_XML)
.HasColumnType("xml");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.Value_Json)
.HasColumnType("jsonb");
modelBuilder.Entity<TelemetryLatest>()
.Property(b => b.Value_XML)
.HasColumnType("xml");
modelBuilder.Entity<AuditLog>()
.Property(b => b.ActionData)
.HasColumnType("jsonb");
modelBuilder.Entity<AuditLog>()
.Property(b => b.ActionResult)
.HasColumnType("jsonb");
}
public DbSet<Tenant> Tenant { get; set; }
public DbSet<Customer> Customer { get; set; }
......
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IoTSharp.Data
{
public interface IDataBaseModelBuilderOptions
{
public void OnModelCreating(ModelBuilder modelBuilder);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册