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

add modify relationship ,add installer api

上级 6a868571
......@@ -64,12 +64,63 @@ namespace IoTSharp.Hub.Controllers
return actionResult;
}
[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> Install([FromBody] InstallDto model)
{
IActionResult actionResult = NoContent();
try
{
var tenant = new Tenant() { Id = Guid.NewGuid(), Name = model.TenantName, EMail = model.TenantEMail };
var customer = new Customer() { Id = Guid.NewGuid(), Name = model.CustomerName, Email = model.CustomerEMail };
customer.Tenant = tenant;
tenant.Customers = new List<Customer>();
tenant.Customers.Add(customer);
tenant.Customers.Add(customer);
var user = new IdentityUser
{
Email = model.AdminEmail,
UserName = model.UserName,
PhoneNumber = model.PhoneNumber
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
_context.Tenant.Add(tenant);
_context.Customer.Add(customer);
await _signInManager.SignInAsync(user, false);
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.Email, model.AdminEmail));
var cust = _context.Customer.FirstOrDefault(c => c.Name == model.CustomerName);
if (cust != null)
{
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.GroupSid, cust.Id.ToString()));
actionResult = Ok(new { code = 0, msg = "OK", data = GenerateJwtToken(model.AdminEmail, user) });
}
}
else
{
var msg = from e in result.Errors select $"{e.Code}:{e.Description}\r\n";
actionResult = BadRequest(new { code = -3, msg = string.Join(';', msg.ToArray()) });
}
}
catch (Exception ex)
{
actionResult = BadRequest(new { code = -2, msg = ex.Message, data = ex });
_logger.LogError(ex, ex.Message);
}
return actionResult;
}
/// <summary>
/// Register a user
/// </summary>
/// <param name="model"></param>
/// <returns ></returns>
/// <seealso cref="BrokerController.InstallCertificate(CertificateDot)"/>
[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> Register([FromBody] RegisterDto model)
......@@ -87,14 +138,14 @@ namespace IoTSharp.Hub.Controllers
{
await _signInManager.SignInAsync(user, false);
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.Email, model.Email));
//var cust = _context.Customer.FirstOrDefault(c => c.Name == model.CustomerName);
//if (cust != null)
//{
// await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.GroupSid,cust.Id.ToString()));
// actionResult = Ok(new { code = 0, msg = "OK", data = GenerateJwtToken(model.Email, user) });
//}
var cust = _context.Customer.FirstOrDefault(c => c.Name == model.CustomerName);
if (cust != null)
{
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.GroupSid, cust.Id.ToString()));
actionResult = Ok(new { code = 0, msg = "OK", data = GenerateJwtToken(model.Email, user) });
}
}
else
{
......@@ -157,5 +208,20 @@ namespace IoTSharp.Hub.Controllers
public string Password { get; set; }
}
public class InstallDto
{
[Required]
public string AdminEmail { get; set; }
[Required]
public string CustomerName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "PASSWORD_MIN_LENGTH", MinimumLength = 6)]
public string Password { get; set; }
public string TenantName { get; set; }
public string TenantEMail { get; set; }
public string CustomerEMail { get; set; }
public string UserName { get; internal set; }
public string PhoneNumber { get; internal set; }
}
}
}
\ No newline at end of file
using IoTSharp.Hub.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace IoTSharp.Hub.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class InstallerController : ControllerBase
{
private ApplicationDbContext _context;
private ILogger _logger;
private readonly UserManager<IdentityUser> _userManager;
private readonly IConfiguration _configuration;
private readonly SignInManager<IdentityUser> _signInManager;
public InstallerController(
UserManager<IdentityUser> userManager,
SignInManager<IdentityUser> signInManager,
IConfiguration configuration, ILogger<AccountController> logger, ApplicationDbContext context
)
{
_userManager = userManager;
_signInManager = signInManager;
_configuration = configuration;
_logger = logger;
_context = context;
}
[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> Install([FromBody] InstallDto model)
{
IActionResult actionResult = NoContent();
var tran = _context.Database.BeginTransaction();
try
{
if (_context.Tenant.Count() == 0 && _context.Customer.Count() == 0)
{
var tenant = new Tenant() { Id = Guid.NewGuid(), Name = model.TenantName, EMail = model.TenantEMail };
var customer = new Customer() { Id = Guid.NewGuid(), Name = model.CustomerName, Email = model.CustomerEMail };
customer.Tenant = tenant;
tenant.Customers = new List<Customer>();
tenant.Customers.Add(customer);
var user = new IdentityUser
{
Email = model.Email,
UserName = model.UserName,
PhoneNumber = model.PhoneNumber
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
_context.Tenant.Add(tenant);
_context.Customer.Add(customer);
await _signInManager.SignInAsync(user, false);
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.Email, model.Email));
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.GroupSid, customer.Id.ToString()));
var rship = new Relationship();
rship.IdentityUser = _context.Users.Find(user.Id);
rship.Customer = customer;
rship.Tenant = tenant;
_context.Add(rship);
int savechangesresult = _context.SaveChanges();
tran.Commit();
actionResult = Ok(new { code = 0, msg = "OK", data = new { result = savechangesresult >= 0, count = savechangesresult } });
}
else
{
tran.Rollback();
var msg = from e in result.Errors select $"{e.Code}:{e.Description}\r\n";
actionResult = BadRequest(new { code = -3, msg = string.Join(';', msg.ToArray()) });
}
}
else
{
tran.Rollback();
actionResult = Ok(new { code = 1, msg = "Already installed" });
}
}
catch (Exception ex)
{
tran.Rollback();
actionResult = BadRequest(new { code = -2, msg = ex.Message, data = ex });
_logger.LogError(ex, ex.Message);
}
return actionResult;
}
public class InstallDto
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string CustomerName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "PASSWORD_MIN_LENGTH", MinimumLength = 6)]
public string Password { get; set; }
public string TenantName { get; set; }
[EmailAddress]
public string TenantEMail { get; set; }
[EmailAddress]
public string CustomerEMail { get; set; }
public string UserName { get; set; }
[Phone]
public string PhoneNumber { get; set; }
}
}
}
\ No newline at end of file
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
......@@ -44,6 +45,7 @@ namespace IoTSharp.Hub.Data
modelBuilder.Entity<TelemetryData>().HasDiscriminator<DataCatalog>(nameof(Data.DataStorage.Catalog));
modelBuilder.Entity<TelemetryLatest>().HasDiscriminator<DataCatalog>(nameof(Data.DataStorage.Catalog));
switch (DatabaseType)
{
......@@ -81,11 +83,11 @@ namespace IoTSharp.Hub.Data
.Property(b => b.Value_XML)
.HasColumnType("xml");
modelBuilder.Entity<DataStorage>()
modelBuilder.Entity<AttributeData>()
.Property(b => b.Value_Json)
.HasColumnType("jsonb");
modelBuilder.Entity<DataStorage>()
modelBuilder.Entity<AttributeData>()
.Property(b => b.Value_XML)
.HasColumnType("xml");
......@@ -119,11 +121,8 @@ namespace IoTSharp.Hub.Data
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; }
......
......@@ -17,7 +17,7 @@ namespace IoTSharp.Hub.Data
public Customer Customer { get; set; }
public virtual List<DataStorage> AttributeData { get; set; }
public virtual List<AttributeData> AttributeData { get; set; }
public virtual List<AttributeLatest> AttributeLatest { get; set; }
public virtual List<TelemetryData> TelemetryData { get; set; }
......
// <auto-generated />
using System;
using IoTSharp.Hub.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace IoTSharp.Hub.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20190104081406_ModifyRelationship")]
partial class ModifyRelationship
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn)
.HasAnnotation("ProductVersion", "2.2.0-rtm-35687")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
modelBuilder.Entity("IoTSharp.Hub.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.Hub.Data.DataStorage", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("Catalog");
b.Property<DateTime>("DateTime");
b.Property<string>("KeyName")
.IsRequired();
b.Property<int>("Type");
b.Property<byte[]>("Value_Binary");
b.Property<bool>("Value_Boolean");
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("Id");
b.ToTable("DataStorage");
b.HasDiscriminator<int>("Catalog").HasValue(0);
});
modelBuilder.Entity("IoTSharp.Hub.Data.Device", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd();
b.Property<Guid?>("CustomerId");
b.Property<string>("Name");
b.Property<Guid?>("TenantId");
b.Property<string>("Type");
b.HasKey("Id");
b.HasIndex("CustomerId");
b.HasIndex("TenantId");
b.ToTable("Device");
});
modelBuilder.Entity("IoTSharp.Hub.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.Hub.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")
.HasMaxLength(128);
b.Property<string>("ProviderKey")
.HasMaxLength(128);
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")
.HasMaxLength(128);
b.Property<string>("Name")
.HasMaxLength(128);
b.Property<string>("Value");
b.HasKey("UserId", "LoginProvider", "Name");
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("IoTSharp.Hub.Data.AttributeData", b =>
{
b.HasBaseType("IoTSharp.Hub.Data.DataStorage");
b.Property<Guid>("DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(1);
});
modelBuilder.Entity("IoTSharp.Hub.Data.AttributeLatest", b =>
{
b.HasBaseType("IoTSharp.Hub.Data.DataStorage");
b.Property<Guid>("DeviceId")
.HasColumnName("AttributeLatest_DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(2);
});
modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryData", b =>
{
b.HasBaseType("IoTSharp.Hub.Data.DataStorage");
b.Property<Guid>("DeviceId")
.HasColumnName("TelemetryData_DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(3);
});
modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryLatest", b =>
{
b.HasBaseType("IoTSharp.Hub.Data.DataStorage");
b.Property<Guid>("DeviceId")
.HasColumnName("TelemetryLatest_DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(4);
});
modelBuilder.Entity("IoTSharp.Hub.Data.Customer", b =>
{
b.HasOne("IoTSharp.Hub.Data.Tenant", "Tenant")
.WithMany("Customers")
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Hub.Data.Device", b =>
{
b.HasOne("IoTSharp.Hub.Data.Customer", "Customer")
.WithMany("Devices")
.HasForeignKey("CustomerId");
b.HasOne("IoTSharp.Hub.Data.Tenant", "Tenant")
.WithMany("Devices")
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Hub.Data.Relationship", b =>
{
b.HasOne("IoTSharp.Hub.Data.Customer", "Customer")
.WithMany()
.HasForeignKey("CustomerId");
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "IdentityUser")
.WithMany()
.HasForeignKey("IdentityUserId");
b.HasOne("IoTSharp.Hub.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);
});
modelBuilder.Entity("IoTSharp.Hub.Data.AttributeData", b =>
{
b.HasOne("IoTSharp.Hub.Data.Device", "Device")
.WithMany("AttributeData")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Hub.Data.AttributeLatest", b =>
{
b.HasOne("IoTSharp.Hub.Data.Device", "Device")
.WithMany("AttributeLatest")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryData", b =>
{
b.HasOne("IoTSharp.Hub.Data.Device", "Device")
.WithMany("TelemetryData")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryLatest", b =>
{
b.HasOne("IoTSharp.Hub.Data.Device", "Device")
.WithMany("TelemetryLatest")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
}
}
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace IoTSharp.Hub.Migrations
{
public partial class ModifyRelationship : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_DataStorage_Device_DeviceId",
table: "DataStorage");
migrationBuilder.DropForeignKey(
name: "FK_DataStorage_Device_DeviceId1",
table: "DataStorage");
migrationBuilder.DropForeignKey(
name: "FK_DataStorage_Device_TelemetryData_DeviceId",
table: "DataStorage");
migrationBuilder.DropForeignKey(
name: "FK_DataStorage_Device_TelemetryLatest_DeviceId",
table: "DataStorage");
migrationBuilder.DropForeignKey(
name: "FK_Relationship_AspNetUsers_IdentityId",
table: "Relationship");
migrationBuilder.DropIndex(
name: "IX_DataStorage_DeviceId1",
table: "DataStorage");
migrationBuilder.DropColumn(
name: "DeviceId1",
table: "DataStorage");
migrationBuilder.RenameColumn(
name: "IdentityId",
table: "Relationship",
newName: "IdentityUserId");
migrationBuilder.RenameIndex(
name: "IX_Relationship_IdentityId",
table: "Relationship",
newName: "IX_Relationship_IdentityUserId");
migrationBuilder.AddColumn<Guid>(
name: "AttributeLatest_DeviceId",
table: "DataStorage",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_DataStorage_AttributeLatest_DeviceId",
table: "DataStorage",
column: "AttributeLatest_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);
migrationBuilder.AddForeignKey(
name: "FK_Relationship_AspNetUsers_IdentityUserId",
table: "Relationship",
column: "IdentityUserId",
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
protected override void Down(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.DropForeignKey(
name: "FK_Relationship_AspNetUsers_IdentityUserId",
table: "Relationship");
migrationBuilder.DropIndex(
name: "IX_DataStorage_AttributeLatest_DeviceId",
table: "DataStorage");
migrationBuilder.DropColumn(
name: "AttributeLatest_DeviceId",
table: "DataStorage");
migrationBuilder.RenameColumn(
name: "IdentityUserId",
table: "Relationship",
newName: "IdentityId");
migrationBuilder.RenameIndex(
name: "IX_Relationship_IdentityUserId",
table: "Relationship",
newName: "IX_Relationship_IdentityId");
migrationBuilder.AddColumn<Guid>(
name: "DeviceId1",
table: "DataStorage",
nullable: false,
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
migrationBuilder.CreateIndex(
name: "IX_DataStorage_DeviceId1",
table: "DataStorage",
column: "DeviceId1");
migrationBuilder.AddForeignKey(
name: "FK_DataStorage_Device_DeviceId",
table: "DataStorage",
column: "DeviceId",
principalTable: "Device",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_DataStorage_Device_DeviceId1",
table: "DataStorage",
column: "DeviceId1",
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.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_DataStorage_Device_TelemetryLatest_DeviceId",
table: "DataStorage",
column: "TelemetryLatest_DeviceId",
principalTable: "Device",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Relationship_AspNetUsers_IdentityId",
table: "Relationship",
column: "IdentityId",
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
}
}
......@@ -60,8 +60,6 @@ namespace IoTSharp.Hub.Migrations
b.Property<DateTime>("DateTime");
b.Property<Guid>("DeviceId1");
b.Property<string>("KeyName")
.IsRequired();
......@@ -85,8 +83,6 @@ namespace IoTSharp.Hub.Migrations
b.HasKey("Id");
b.HasIndex("DeviceId1");
b.ToTable("DataStorage");
b.HasDiscriminator<int>("Catalog").HasValue(0);
......@@ -121,7 +117,7 @@ namespace IoTSharp.Hub.Migrations
b.Property<Guid?>("CustomerId");
b.Property<string>("IdentityId");
b.Property<string>("IdentityUserId");
b.Property<Guid?>("TenantId");
......@@ -129,7 +125,7 @@ namespace IoTSharp.Hub.Migrations
b.HasIndex("CustomerId");
b.HasIndex("IdentityId");
b.HasIndex("IdentityUserId");
b.HasIndex("TenantId");
......@@ -329,6 +325,10 @@ namespace IoTSharp.Hub.Migrations
{
b.HasBaseType("IoTSharp.Hub.Data.DataStorage");
b.Property<Guid>("DeviceId");
b.HasIndex("DeviceId");
b.HasDiscriminator().HasValue(1);
});
......@@ -336,7 +336,8 @@ namespace IoTSharp.Hub.Migrations
{
b.HasBaseType("IoTSharp.Hub.Data.DataStorage");
b.Property<Guid?>("DeviceId");
b.Property<Guid>("DeviceId")
.HasColumnName("AttributeLatest_DeviceId");
b.HasIndex("DeviceId");
......@@ -347,7 +348,7 @@ namespace IoTSharp.Hub.Migrations
{
b.HasBaseType("IoTSharp.Hub.Data.DataStorage");
b.Property<Guid?>("DeviceId")
b.Property<Guid>("DeviceId")
.HasColumnName("TelemetryData_DeviceId");
b.HasIndex("DeviceId");
......@@ -359,7 +360,7 @@ namespace IoTSharp.Hub.Migrations
{
b.HasBaseType("IoTSharp.Hub.Data.DataStorage");
b.Property<Guid?>("DeviceId")
b.Property<Guid>("DeviceId")
.HasColumnName("TelemetryLatest_DeviceId");
b.HasIndex("DeviceId");
......@@ -374,14 +375,6 @@ namespace IoTSharp.Hub.Migrations
.HasForeignKey("TenantId");
});
modelBuilder.Entity("IoTSharp.Hub.Data.DataStorage", b =>
{
b.HasOne("IoTSharp.Hub.Data.Device", "Device")
.WithMany("AttributeData")
.HasForeignKey("DeviceId1")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Hub.Data.Device", b =>
{
b.HasOne("IoTSharp.Hub.Data.Customer", "Customer")
......@@ -399,9 +392,9 @@ namespace IoTSharp.Hub.Migrations
.WithMany()
.HasForeignKey("CustomerId");
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "Identity")
b.HasOne("Microsoft.AspNetCore.Identity.IdentityUser", "IdentityUser")
.WithMany()
.HasForeignKey("IdentityId");
.HasForeignKey("IdentityUserId");
b.HasOne("IoTSharp.Hub.Data.Tenant", "Tenant")
.WithMany()
......@@ -453,25 +446,36 @@ namespace IoTSharp.Hub.Migrations
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Hub.Data.AttributeData", b =>
{
b.HasOne("IoTSharp.Hub.Data.Device", "Device")
.WithMany("AttributeData")
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Hub.Data.AttributeLatest", b =>
{
b.HasOne("IoTSharp.Hub.Data.Device")
b.HasOne("IoTSharp.Hub.Data.Device", "Device")
.WithMany("AttributeLatest")
.HasForeignKey("DeviceId");
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryData", b =>
{
b.HasOne("IoTSharp.Hub.Data.Device")
b.HasOne("IoTSharp.Hub.Data.Device", "Device")
.WithMany("TelemetryData")
.HasForeignKey("DeviceId");
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("IoTSharp.Hub.Data.TelemetryLatest", b =>
{
b.HasOne("IoTSharp.Hub.Data.Device")
b.HasOne("IoTSharp.Hub.Data.Device", "Device")
.WithMany("TelemetryLatest")
.HasForeignKey("DeviceId");
.HasForeignKey("DeviceId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
......@@ -9,11 +10,11 @@ namespace IoTSharp.Hub.Data
{
public class Relationship
{
[Key]
public Guid Id { get; set; }
public virtual IdentityUser Identity { get; set; }
public IdentityUser IdentityUser { get; set; }
public virtual Tenant Tenant { get; set; }
public virtual Customer Customer { get; set; }
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册