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

DataStorage changed to composite primary key

上级 94c71471
......@@ -82,12 +82,12 @@ namespace IoTSharp.Controllers
[HttpGet("{deviceId}/AttributeLatest")]
public async Task<ActionResult<List<AttributeLatest>>> GetAttributeLatest(Guid deviceId)
{
var devid = from dev in _context.Device.Include(d =>d.AttributeLatest) where dev.Id == deviceId select dev.AttributeLatest;
var devid = from dev in _context.AttributeLatest where dev.DeviceId == deviceId select dev ;
if (!devid.Any())
{
return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found "));
}
return await devid.FirstOrDefaultAsync();
return await devid.ToListAsync();
}
/// <summary>
......@@ -99,12 +99,12 @@ namespace IoTSharp.Controllers
[HttpGet("{deviceId}/TelemetryLatest")]
public async Task<ActionResult<List<TelemetryLatest>>> GetTelemetryLatest(Guid deviceId)
{
var devid = from dev in _context.Device.Include(d=>d.TelemetryLatest) where dev.Id == deviceId select dev.TelemetryLatest;
var devid = from dev in _context.TelemetryLatest where dev.DeviceId == deviceId select dev;
if (!devid.Any())
{
return NotFound(new ApiResult(ApiCode.NotFoundDeviceIdentity, $"Device's Identity not found "));
}
return await devid.FirstOrDefaultAsync();
return await devid.ToListAsync();
}
/// <summary>
/// Request telemetry values from the server
......@@ -123,7 +123,7 @@ namespace IoTSharp.Controllers
}
else
{
var kv = from t in _context.TelemetryLatest where t.Device == dev && t.KeyName == keyName select t;
var kv = from t in _context.TelemetryLatest where t.DeviceId == dev.Id && t.KeyName == keyName select t;
return (await kv.FirstOrDefaultAsync())?.ToObject();
}
}
......@@ -144,7 +144,7 @@ namespace IoTSharp.Controllers
}
else
{
var kv = from t in _context.AttributeLatest where t.Device == dev && t.KeyName == keyName select t;
var kv = from t in _context.AttributeLatest where t.DeviceId == dev.Id && t.KeyName == keyName select t;
return (await kv.FirstOrDefaultAsync())?.ToObject();
}
}
......@@ -167,7 +167,7 @@ namespace IoTSharp.Controllers
}
else
{
var kv = from t in _context.TelemetryLatest where t.Device == dev && t.KeyName == keyName && t.DateTime >= begin select t.ToObject();
var kv = from t in _context.TelemetryLatest where t.DeviceId == dev.Id && t.KeyName == keyName && t.DateTime >= begin select t.ToObject();
return await kv.ToArrayAsync();
}
}
......@@ -190,7 +190,7 @@ namespace IoTSharp.Controllers
}
else
{
var kv = from t in _context.TelemetryLatest where t.Device == dev && t.KeyName == keyName && t.DateTime>=begin && t.DateTime <end select t.ToObject() ;
var kv = from t in _context.TelemetryLatest where t.DeviceId == dev.Id && t.KeyName == keyName && t.DateTime>=begin && t.DateTime <end select t.ToObject() ;
return await kv.ToArrayAsync();
}
}
......@@ -403,9 +403,8 @@ namespace IoTSharp.Controllers
var deviceId = device.Id;
try
{
var attributes = from dev in _context.Device where dev.Id == deviceId select dev.AttributeLatest;
var arrys = await attributes.FirstOrDefaultAsync();
var fs = from at in arrys.ToArray() where at.DataSide == dataSide && keys.Split(',', options: StringSplitOptions.RemoveEmptyEntries).Contains(at.KeyName) select at;
var attributes = from dev in _context.AttributeLatest where dev.DeviceId == deviceId select dev;
var fs = from at in await attributes.ToListAsync() where at.DataSide == dataSide && keys.Split(',', options: StringSplitOptions.RemoveEmptyEntries).Contains(at.KeyName) select at;
return Ok(fs.ToArray());
}
catch (Exception ex)
......
......@@ -30,7 +30,11 @@ namespace IoTSharp.Data
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<DataStorage>().HasKey(c => new { c.Catalog, c.DeviceId, c.KeyName, c.DateTime });
modelBuilder.Entity<DataStorage>().HasIndex(c => c.Catalog);
modelBuilder.Entity<DataStorage>().HasIndex(c => new { c.Catalog, c.DeviceId });
modelBuilder.Entity<DataStorage>().HasIndex(c => new {c.Catalog,c.DeviceId, c.KeyName } );
modelBuilder.Entity<DataStorage>().HasIndex(c => new { c.Catalog, c.DeviceId, c.KeyName,c.DateTime });
modelBuilder.Entity<DataStorage>()
.HasDiscriminator<DataCatalog>(nameof(Data.DataStorage.Catalog))
.HasValue<DataStorage>(DataCatalog.None)
......
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace IoTSharp.Data
{
public class DataStorage
{
[Key]
public Guid Id { get; set; } = Guid.NewGuid();
[EnumDataType(typeof(DataCatalog)), Column(Order = 0)]
public DataCatalog Catalog { get; set; }
[Newtonsoft.Json.JsonIgnore, Column(Order = 1)]
public Guid DeviceId { get; set; }
[Required]
[Column(Order = 2)]
public string KeyName { get; set; }
[Newtonsoft.Json.JsonIgnore]
[Required]
public Device Device { get; set; }
[Column(Order = 3)]
public DateTime DateTime { get; set; }
[Required]
[Column(Order = 4)]
[EnumDataType(typeof(DataSide))]
public DataSide DataSide { get; set; } = DataSide.AnySide;
[Required]
[EnumDataType(typeof(DataCatalog))]
public DataCatalog Catalog { get; set; }
[Required]
[Column(Order = 5)]
[EnumDataType(typeof(DataType))]
public DataType Type { get; set; }
public DateTime DateTime { get; set; }
public bool Value_Boolean { get; set; }
public string Value_String { get; set; }
public long Value_Long { get; set; }
......
......@@ -15,14 +15,5 @@ namespace IoTSharp.Data
public Customer Customer { get; set; }
[Newtonsoft.Json.JsonIgnore]
public virtual List<AttributeData> AttributeData { get; set; }
[Newtonsoft.Json.JsonIgnore]
public virtual List<TelemetryData> TelemetryData { get; set; }
public virtual List<AttributeLatest> AttributeLatest { get; set; }
public virtual List<TelemetryLatest> TelemetryLatest { get; set; }
}
}
\ No newline at end of file
// <auto-generated />
using System;
using IoTSharp.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace IoTSharp.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20190903070558_ReDesingerDataStorage")]
partial class ReDesingerDataStorage
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn)
.HasAnnotation("ProductVersion", "2.2.6-servicing-10079")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
modelBuilder.Entity("IoTSharp.Data.AuditLog", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ActionData")
.HasColumnType("jsonb");
b.Property<string>("ActionName");
b.Property<string>("ActionResult")
.HasColumnType("jsonb");
b.Property<DateTime>("ActiveDateTime");
b.Property<Guid?>("CustomerId");
b.Property<Guid>("ObjectID");
b.Property<string>("ObjectName");
b.Property<int>("ObjectType");
b.Property<Guid?>("TenantId");
b.Property<string>("UserId");
b.Property<string>("UserName");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("TenantId");
b.ToTable("AuditLog");
});
modelBuilder.Entity("IoTSharp.Data.Customer", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Address");
b.Property<string>("City");
b.Property<string>("Country");
b.Property<string>("Email");
b.Property<string>("Name");
b.Property<string>("Phone");
b.Property<string>("Province");
b.Property<string>("Street");
b.Property<Guid?>("TenantId");
b.Property<int>("ZipCode");
b.HasKey("Id");
b.HasIndex("TenantId");
b.ToTable("Customer");
});
modelBuilder.Entity("IoTSharp.Data.DataStorage", b =>
{
b.Property<int>("Catalog");
b.Property<Guid>("DeviceId");
b.Property<string>("KeyName");
b.Property<DateTime>("DateTime");
b.Property<int>("DataSide");
b.Property<int>("Type");
b.Property<byte[]>("Value_Binary");
b.Property<bool>("Value_Boolean");
b.Property<DateTime>("Value_DateTime");
b.Property<double>("Value_Double");
b.Property<string>("Value_Json")
.HasColumnType("jsonb");
b.Property<long>("Value_Long");
b.Property<string>("Value_String");
b.Property<string>("Value_XML")
.HasColumnType("xml");
b.HasKey("Catalog", "DeviceId", "KeyName", "DateTime");
b.HasIndex("Catalog");
b.HasIndex("Catalog", "DeviceId");
b.HasIndex("Catalog", "DeviceId", "KeyName");
b.HasIndex("Catalog", "DeviceId", "KeyName", "DateTime");
b.ToTable("DataStorage");
b.HasDiscriminator<int>("Catalog").HasValue(0);
});
modelBuilder.Entity("IoTSharp.Data.Device", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid?>("CustomerId");
b.Property<int>("DeviceType");
b.Property<string>("Name");
b.Property<Guid?>("OwnerId");
b.Property<Guid?>("TenantId");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("OwnerId");
b.HasIndex("TenantId");
b.ToTable("Device");
b.HasDiscriminator<int>("DeviceType").HasValue(0);
});
modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid>("DeviceId");
b.Property<string>("IdentityId")
.IsRequired();
b.Property<int>("IdentityType");
b.Property<string>("IdentityValue");
b.HasKey("Id");
b.HasIndex("DeviceId");
b.ToTable("DeviceIdentities");
});
modelBuilder.Entity("IoTSharp.Data.Relationship", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid?>("CustomerId");
b.Property<string>("IdentityUserId");
b.Property<Guid?>("TenantId");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("IdentityUserId");
b.HasIndex("TenantId");
b.ToTable("Relationship");
});
modelBuilder.Entity("IoTSharp.Data.RetainedMessage", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<byte[]>("Payload");
b.Property<int>("QualityOfServiceLevel");
b.Property<bool>("Retain");
b.Property<string>("Topic");
b.HasKey("Id");
b.ToTable("RetainedMessage");
});
modelBuilder.Entity("IoTSharp.Data.Tenant", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Address");
b.Property<string>("City");
b.Property<string>("Country");
b.Property<string>("EMail");
b.Property<string>("Name");
b.Property<string>("Phone");
b.Property<string>("Province");
b.Property<string>("Street");
b.Property<int>("ZipCode");
b.HasKey("Id");
b.ToTable("Tenant");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex");
b.ToTable("AspNetRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("RoleId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("RoleId");
b.ToTable("AspNetRoleClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("AccessFailedCount");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Email")
.HasMaxLength(256);
b.Property<bool>("EmailConfirmed");
b.Property<bool>("LockoutEnabled");
b.Property<DateTimeOffset?>("LockoutEnd");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256);
b.Property<string>("NormalizedUserName")
.HasMaxLength(256);
b.Property<string>("PasswordHash");
b.Property<string>("PhoneNumber");
b.Property<bool>("PhoneNumberConfirmed");
b.Property<string>("SecurityStamp");
b.Property<bool>("TwoFactorEnabled");
b.Property<string>("UserName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedEmail")
.HasName("EmailIndex");
b.HasIndex("NormalizedUserName")
.IsUnique()
.HasName("UserNameIndex");
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("AspNetUserClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider");
b.Property<string>("ProviderKey");
b.Property<string>("ProviderDisplayName");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("LoginProvider", "ProviderKey");
b.HasIndex("UserId");
b.ToTable("AspNetUserLogins");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("RoleId");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("LoginProvider");
b.Property<string>("Name");
b.Property<string>("Value");
b.HasKey("UserId", "LoginProvider", "Name");
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("IoTSharp.Data.AttributeData", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.HasDiscriminator().HasValue(1);
});
modelBuilder.Entity("IoTSharp.Data.AttributeLatest", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.HasDiscriminator().HasValue(2);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryData", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.HasDiscriminator().HasValue(3);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryLatest", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.HasDiscriminator().HasValue(4);
});
modelBuilder.Entity("IoTSharp.Data.Gateway", b =>
{
b.HasBaseType("IoTSharp.Data.Device");
b.HasDiscriminator().HasValue(1);
});
modelBuilder.Entity("IoTSharp.Data.AuditLog", b =>
{
b.HasOne("IoTSharp.Data.Customer", "Customer")
.WithMany()
.HasForeignKey("CustomerId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany()
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Data.Customer", b =>
{
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany("Customers")
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Data.Device", b =>
{
b.HasOne("IoTSharp.Data.Customer", "Customer")
.WithMany("Devices")
.HasForeignKey("CustomerId");
b.HasOne("IoTSharp.Data.Gateway", "Owner")
.WithMany("Children")
.HasForeignKey("OwnerId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany("Devices")
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany()
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.Relationship", b =>
{
b.HasOne("IoTSharp.Data.Customer", "Customer")
.WithMany()
.HasForeignKey("CustomerId");
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "IdentityUser")
.WithMany()
.HasForeignKey("IdentityUserId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany()
.HasForeignKey("TenantId");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
}
}
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace IoTSharp.Migrations
{
public partial class ReDesingerDataStorage : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_DataStorage_Device_DeviceId",
table: "DataStorage");
migrationBuilder.DropForeignKey(
name: "FK_DataStorage_Device_AttributeLatest_DeviceId",
table: "DataStorage");
migrationBuilder.DropForeignKey(
name: "FK_DataStorage_Device_TelemetryData_DeviceId",
table: "DataStorage");
migrationBuilder.DropForeignKey(
name: "FK_DataStorage_Device_TelemetryLatest_DeviceId",
table: "DataStorage");
migrationBuilder.DropPrimaryKey(
name: "PK_DataStorage",
table: "DataStorage");
migrationBuilder.DropIndex(
name: "IX_DataStorage_DeviceId",
table: "DataStorage");
migrationBuilder.DropIndex(
name: "IX_DataStorage_AttributeLatest_DeviceId",
table: "DataStorage");
migrationBuilder.DropIndex(
name: "IX_DataStorage_TelemetryData_DeviceId",
table: "DataStorage");
migrationBuilder.DropIndex(
name: "IX_DataStorage_TelemetryLatest_DeviceId",
table: "DataStorage");
migrationBuilder.DropColumn(
name: "AttributeLatest_DeviceId",
table: "DataStorage");
migrationBuilder.DropColumn(
name: "Id",
table: "DataStorage");
migrationBuilder.DropColumn(
name: "TelemetryData_DeviceId",
table: "DataStorage");
migrationBuilder.DropColumn(
name: "TelemetryLatest_DeviceId",
table: "DataStorage");
migrationBuilder.AlterColumn<Guid>(
name: "DeviceId",
table: "DataStorage",
nullable: false,
oldClrType: typeof(Guid),
oldNullable: true);
migrationBuilder.AddPrimaryKey(
name: "PK_DataStorage",
table: "DataStorage",
columns: new[] { "Catalog", "DeviceId", "KeyName", "DateTime" });
migrationBuilder.CreateIndex(
name: "IX_DataStorage_Catalog",
table: "DataStorage",
column: "Catalog");
migrationBuilder.CreateIndex(
name: "IX_DataStorage_Catalog_DeviceId",
table: "DataStorage",
columns: new[] { "Catalog", "DeviceId" });
migrationBuilder.CreateIndex(
name: "IX_DataStorage_Catalog_DeviceId_KeyName",
table: "DataStorage",
columns: new[] { "Catalog", "DeviceId", "KeyName" });
migrationBuilder.CreateIndex(
name: "IX_DataStorage_Catalog_DeviceId_KeyName_DateTime",
table: "DataStorage",
columns: new[] { "Catalog", "DeviceId", "KeyName", "DateTime" });
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_DataStorage",
table: "DataStorage");
migrationBuilder.DropIndex(
name: "IX_DataStorage_Catalog",
table: "DataStorage");
migrationBuilder.DropIndex(
name: "IX_DataStorage_Catalog_DeviceId",
table: "DataStorage");
migrationBuilder.DropIndex(
name: "IX_DataStorage_Catalog_DeviceId_KeyName",
table: "DataStorage");
migrationBuilder.DropIndex(
name: "IX_DataStorage_Catalog_DeviceId_KeyName_DateTime",
table: "DataStorage");
migrationBuilder.AlterColumn<Guid>(
name: "DeviceId",
table: "DataStorage",
nullable: true,
oldClrType: typeof(Guid));
migrationBuilder.AddColumn<Guid>(
name: "AttributeLatest_DeviceId",
table: "DataStorage",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "Id",
table: "DataStorage",
nullable: false,
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
migrationBuilder.AddColumn<Guid>(
name: "TelemetryData_DeviceId",
table: "DataStorage",
nullable: true);
migrationBuilder.AddColumn<Guid>(
name: "TelemetryLatest_DeviceId",
table: "DataStorage",
nullable: true);
migrationBuilder.AddPrimaryKey(
name: "PK_DataStorage",
table: "DataStorage",
column: "Id");
migrationBuilder.CreateIndex(
name: "IX_DataStorage_DeviceId",
table: "DataStorage",
column: "DeviceId");
migrationBuilder.CreateIndex(
name: "IX_DataStorage_AttributeLatest_DeviceId",
table: "DataStorage",
column: "AttributeLatest_DeviceId");
migrationBuilder.CreateIndex(
name: "IX_DataStorage_TelemetryData_DeviceId",
table: "DataStorage",
column: "TelemetryData_DeviceId");
migrationBuilder.CreateIndex(
name: "IX_DataStorage_TelemetryLatest_DeviceId",
table: "DataStorage",
column: "TelemetryLatest_DeviceId");
migrationBuilder.AddForeignKey(
name: "FK_DataStorage_Device_DeviceId",
table: "DataStorage",
column: "DeviceId",
principalTable: "Device",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_DataStorage_Device_AttributeLatest_DeviceId",
table: "DataStorage",
column: "AttributeLatest_DeviceId",
principalTable: "Device",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_DataStorage_Device_TelemetryData_DeviceId",
table: "DataStorage",
column: "TelemetryData_DeviceId",
principalTable: "Device",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_DataStorage_Device_TelemetryLatest_DeviceId",
table: "DataStorage",
column: "TelemetryLatest_DeviceId",
principalTable: "Device",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
}
}
// <auto-generated />
using System;
using IoTSharp.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace IoTSharp.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20190903070959_ModifyColumnOrder")]
partial class ModifyColumnOrder
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn)
.HasAnnotation("ProductVersion", "2.2.6-servicing-10079")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
modelBuilder.Entity("IoTSharp.Data.AuditLog", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ActionData")
.HasColumnType("jsonb");
b.Property<string>("ActionName");
b.Property<string>("ActionResult")
.HasColumnType("jsonb");
b.Property<DateTime>("ActiveDateTime");
b.Property<Guid?>("CustomerId");
b.Property<Guid>("ObjectID");
b.Property<string>("ObjectName");
b.Property<int>("ObjectType");
b.Property<Guid?>("TenantId");
b.Property<string>("UserId");
b.Property<string>("UserName");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("TenantId");
b.ToTable("AuditLog");
});
modelBuilder.Entity("IoTSharp.Data.Customer", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Address");
b.Property<string>("City");
b.Property<string>("Country");
b.Property<string>("Email");
b.Property<string>("Name");
b.Property<string>("Phone");
b.Property<string>("Province");
b.Property<string>("Street");
b.Property<Guid?>("TenantId");
b.Property<int>("ZipCode");
b.HasKey("Id");
b.HasIndex("TenantId");
b.ToTable("Customer");
});
modelBuilder.Entity("IoTSharp.Data.DataStorage", b =>
{
b.Property<int>("Catalog");
b.Property<Guid>("DeviceId");
b.Property<string>("KeyName");
b.Property<DateTime>("DateTime");
b.Property<int>("DataSide");
b.Property<int>("Type");
b.Property<byte[]>("Value_Binary");
b.Property<bool>("Value_Boolean");
b.Property<DateTime>("Value_DateTime");
b.Property<double>("Value_Double");
b.Property<string>("Value_Json")
.HasColumnType("jsonb");
b.Property<long>("Value_Long");
b.Property<string>("Value_String");
b.Property<string>("Value_XML")
.HasColumnType("xml");
b.HasKey("Catalog", "DeviceId", "KeyName", "DateTime");
b.HasIndex("Catalog");
b.HasIndex("Catalog", "DeviceId");
b.HasIndex("Catalog", "DeviceId", "KeyName");
b.HasIndex("Catalog", "DeviceId", "KeyName", "DateTime");
b.ToTable("DataStorage");
b.HasDiscriminator<int>("Catalog").HasValue(0);
});
modelBuilder.Entity("IoTSharp.Data.Device", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid?>("CustomerId");
b.Property<int>("DeviceType");
b.Property<string>("Name");
b.Property<Guid?>("OwnerId");
b.Property<Guid?>("TenantId");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("OwnerId");
b.HasIndex("TenantId");
b.ToTable("Device");
b.HasDiscriminator<int>("DeviceType").HasValue(0);
});
modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid>("DeviceId");
b.Property<string>("IdentityId")
.IsRequired();
b.Property<int>("IdentityType");
b.Property<string>("IdentityValue");
b.HasKey("Id");
b.HasIndex("DeviceId");
b.ToTable("DeviceIdentities");
});
modelBuilder.Entity("IoTSharp.Data.Relationship", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid?>("CustomerId");
b.Property<string>("IdentityUserId");
b.Property<Guid?>("TenantId");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("IdentityUserId");
b.HasIndex("TenantId");
b.ToTable("Relationship");
});
modelBuilder.Entity("IoTSharp.Data.RetainedMessage", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<byte[]>("Payload");
b.Property<int>("QualityOfServiceLevel");
b.Property<bool>("Retain");
b.Property<string>("Topic");
b.HasKey("Id");
b.ToTable("RetainedMessage");
});
modelBuilder.Entity("IoTSharp.Data.Tenant", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Address");
b.Property<string>("City");
b.Property<string>("Country");
b.Property<string>("EMail");
b.Property<string>("Name");
b.Property<string>("Phone");
b.Property<string>("Province");
b.Property<string>("Street");
b.Property<int>("ZipCode");
b.HasKey("Id");
b.ToTable("Tenant");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex");
b.ToTable("AspNetRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("RoleId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("RoleId");
b.ToTable("AspNetRoleClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUser", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("AccessFailedCount");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<string>("Email")
.HasMaxLength(256);
b.Property<bool>("EmailConfirmed");
b.Property<bool>("LockoutEnabled");
b.Property<DateTimeOffset?>("LockoutEnd");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256);
b.Property<string>("NormalizedUserName")
.HasMaxLength(256);
b.Property<string>("PasswordHash");
b.Property<string>("PhoneNumber");
b.Property<bool>("PhoneNumberConfirmed");
b.Property<string>("SecurityStamp");
b.Property<bool>("TwoFactorEnabled");
b.Property<string>("UserName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("NormalizedEmail")
.HasName("EmailIndex");
b.HasIndex("NormalizedUserName")
.IsUnique()
.HasName("UserNameIndex");
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("AspNetUserClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider");
b.Property<string>("ProviderKey");
b.Property<string>("ProviderDisplayName");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("LoginProvider", "ProviderKey");
b.HasIndex("UserId");
b.ToTable("AspNetUserLogins");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("RoleId");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("LoginProvider");
b.Property<string>("Name");
b.Property<string>("Value");
b.HasKey("UserId", "LoginProvider", "Name");
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("IoTSharp.Data.AttributeData", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.HasDiscriminator().HasValue(1);
});
modelBuilder.Entity("IoTSharp.Data.AttributeLatest", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.HasDiscriminator().HasValue(2);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryData", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.HasDiscriminator().HasValue(3);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryLatest", b =>
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.HasDiscriminator().HasValue(4);
});
modelBuilder.Entity("IoTSharp.Data.Gateway", b =>
{
b.HasBaseType("IoTSharp.Data.Device");
b.HasDiscriminator().HasValue(1);
});
modelBuilder.Entity("IoTSharp.Data.AuditLog", b =>
{
b.HasOne("IoTSharp.Data.Customer", "Customer")
.WithMany()
.HasForeignKey("CustomerId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany()
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Data.Customer", b =>
{
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany("Customers")
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Data.Device", b =>
{
b.HasOne("IoTSharp.Data.Customer", "Customer")
.WithMany("Devices")
.HasForeignKey("CustomerId");
b.HasOne("IoTSharp.Data.Gateway", "Owner")
.WithMany("Children")
.HasForeignKey("OwnerId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany("Devices")
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Data.DeviceIdentity", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany()
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.Relationship", b =>
{
b.HasOne("IoTSharp.Data.Customer", "Customer")
.WithMany()
.HasForeignKey("CustomerId");
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "IdentityUser")
.WithMany()
.HasForeignKey("IdentityUserId");
b.HasOne("IoTSharp.Data.Tenant", "Tenant")
.WithMany()
.HasForeignKey("TenantId");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace IoTSharp.Migrations
{
public partial class ModifyColumnOrder : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}
......@@ -16,7 +16,7 @@ namespace IoTSharp.Migrations
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn)
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062")
.HasAnnotation("ProductVersion", "2.2.6-servicing-10079")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
modelBuilder.Entity("IoTSharp.Data.AuditLog", b =>
......@@ -91,17 +91,15 @@ namespace IoTSharp.Migrations
modelBuilder.Entity("IoTSharp.Data.DataStorage", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("Catalog");
b.Property<int>("DataSide");
b.Property<Guid>("DeviceId");
b.Property<string>("KeyName");
b.Property<DateTime>("DateTime");
b.Property<string>("KeyName")
.IsRequired();
b.Property<int>("DataSide");
b.Property<int>("Type");
......@@ -123,7 +121,15 @@ namespace IoTSharp.Migrations
b.Property<string>("Value_XML")
.HasColumnType("xml");
b.HasKey("Id");
b.HasKey("Catalog", "DeviceId", "KeyName", "DateTime");
b.HasIndex("Catalog");
b.HasIndex("Catalog", "DeviceId");
b.HasIndex("Catalog", "DeviceId", "KeyName");
b.HasIndex("Catalog", "DeviceId", "KeyName", "DateTime");
b.ToTable("DataStorage");
......@@ -408,10 +414,6 @@ namespace IoTSharp.Migrations
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.Property<Guid>("DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(1);
});
......@@ -419,11 +421,6 @@ namespace IoTSharp.Migrations
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.Property<Guid>("DeviceId")
.HasColumnName("AttributeLatest_DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(2);
});
......@@ -431,11 +428,6 @@ namespace IoTSharp.Migrations
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.Property<Guid>("DeviceId")
.HasColumnName("TelemetryData_DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(3);
});
......@@ -443,11 +435,6 @@ namespace IoTSharp.Migrations
{
b.HasBaseType("IoTSharp.Data.DataStorage");
b.Property<Guid>("DeviceId")
.HasColumnName("TelemetryLatest_DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(4);
});
......@@ -558,38 +545,6 @@ namespace IoTSharp.Migrations
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.AttributeData", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany("AttributeData")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.AttributeLatest", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany("AttributeLatest")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryData", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany("TelemetryData")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Data.TelemetryLatest", b =>
{
b.HasOne("IoTSharp.Data.Device", "Device")
.WithMany("TelemetryLatest")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
}
......
......@@ -54,14 +54,13 @@ namespace IoTSharp.Extensions
{
try
{
var tdata = new D() { DateTime = DateTime.Now, Device = device, Id = Guid.NewGuid(), KeyName = kp.Key };
var tdata = new D() { DateTime = DateTime.Now, DeviceId = device.Id, KeyName = kp.Key };
if (kp.Key != null)
{
tdata.FillKVToMe(kp);
tdata.Id = Guid.NewGuid();
_context.Set<D>().Add(tdata);
}
var tl = _context.Set<L>().FirstOrDefault(tx => tx.Device.Id == device.Id && tx.KeyName == kp.Key && tx.DataSide==dataSide);
var tl = _context.Set<L>().FirstOrDefault(tx => tx.DeviceId == device.Id && tx.KeyName == kp.Key && tx.DataSide==dataSide);
if (tl != null)
{
tl.FillKVToMe(kp);
......@@ -69,7 +68,7 @@ namespace IoTSharp.Extensions
}
else
{
var t2 = new L() { DateTime = DateTime.Now, Device = device, Id = Guid.NewGuid(), KeyName = kp.Key, DataSide = dataSide };
var t2 = new L() { DateTime = DateTime.Now, DeviceId = device.Id, KeyName = kp.Key, DataSide = dataSide };
t2.FillKVToMe(kp);
_context.Set<L>().Add(t2);
}
......
......@@ -206,7 +206,7 @@ namespace IoTSharp.Handlers
{
if (Enum.TryParse(kx.Key, true, out DataSide ds))
{
var qf = from at in _dbContext.AttributeLatest.Include(al => al.Device) where at.Device == device && keys.Contains(at.KeyName) select at;
var qf = from at in _dbContext.AttributeLatest where at.DeviceId == device.Id && keys.Contains(at.KeyName) select at;
await qf.LoadAsync();
if (ds == DataSide.AnySide)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册