You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoT.Shared/IoTSharedDbConfig.cs

27 lines
1.4 KiB

using Infrastructure.Application.Entites.Settings;
using Application.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace IoT.Shared
{
public class IoTSharedDbConfig
{
public static void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Setting>().Property(o=>o.Name).IsRequired();
modelBuilder.Entity<Setting>().HasIndex(o => o.Name).IsUnique();
//
modelBuilder.Entity<IoTGateway>().HasIndex(o => o.Number).IsUnique();
//
modelBuilder.Entity<IoTProduct>().HasIndex(o => o.Number).IsUnique();
modelBuilder.Entity<IoTApi>().HasOne(o => o.Product).WithMany(o => o.IoTApis).HasForeignKey(o => o.IoTProductId);
modelBuilder.Entity<IoTApi>().HasIndex(o => new { o.IoTProductId, o.Name }).IsUnique();
modelBuilder.Entity<IoTParameter>().HasOne(o => o.IoTApi).WithMany(o => o.IoTParameters).HasForeignKey(o => o.IoTApiId);
//
modelBuilder.Entity<IoTDevice>().HasOne(o => o.IoTProduct).WithMany(o => o.IoTDevices).HasForeignKey(o => o.IoTProductId);
modelBuilder.Entity<IoTDevice>().HasOne(o => o.IoTGateway).WithMany(o => o.Devices).HasForeignKey(o => o.IoTGatewayId);
modelBuilder.Entity<IoTDevice>().HasIndex(o => o.Number).IsUnique();
modelBuilder.Entity<IoTData>().HasOne(o => o.IoTDevice).WithMany(o => o.Data).HasForeignKey(o => o.IoTDeviceId);
}
}
}