初始化数据库表配置

This commit is contained in:
2024-10-25 00:28:52 +08:00
committed by 南浔
parent 7b22e66310
commit 89d74e83b1
27 changed files with 1723 additions and 48 deletions
@@ -0,0 +1,26 @@
using Apimanager_backend.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Apimanager_backend.Data
{
public class ApiCallLogConfig : IEntityTypeConfiguration<ApiCallLog>
{
public void Configure(EntityTypeBuilder<ApiCallLog> builder)
{
//主键
builder.HasKey(x => x.Id);
//自增
builder.Property(x => x.Id)
.ValueGeneratedOnAdd();
//外键:API
builder.HasOne(x => x.Api)
.WithMany(u => u.ApiCalls)
.HasForeignKey(x => x.ApiId);
//外键:User
builder.HasOne(x => x.User)
.WithMany(u => u.CallLogs)
.HasForeignKey(x => x.UserId);
}
}
}
+23
View File
@@ -0,0 +1,23 @@
using Apimanager_backend.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Apimanager_backend.Data
{
public class ApiConfig : IEntityTypeConfiguration<Api>
{
public void Configure(EntityTypeBuilder<Api> builder)
{
//主键
builder.HasKey(x => x.Id);
//自增
builder.Property(x => x.Id)
.ValueGeneratedOnAdd();
//外键
builder.HasOne(x => x.Package)
.WithMany(u => u.Apis)
.HasForeignKey(x => x.PackageId);
}
}
}
+30
View File
@@ -0,0 +1,30 @@
using Apimanager_backend.Models;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
namespace Apimanager_backend.Data
{
public class ApiContext:DbContext
{
//API表
public DbSet<Api> Apis { get; set; }
//用户表
public DbSet<User> Users { get; set; }
//API调用日志
public DbSet<ApiCallLog> CallLogs { get; set; }
//套餐表
public DbSet<Apipackage> Apipackages { get; set; }
//操作日志表
public DbSet<OperationLog> OperationLogs { get; set; }
//订单表
public DbSet<Order> Orders { get; set; }
//用户已订购套餐表
public DbSet<UserPackage> UserPackages { get; set; }
public ApiContext(DbContextOptions<ApiContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApiContext).Assembly);
}
}
}
@@ -0,0 +1,22 @@
using Apimanager_backend.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Apimanager_backend.Data
{
public class OperationLogConfig : IEntityTypeConfiguration<OperationLog>
{
public void Configure(EntityTypeBuilder<OperationLog> builder)
{
//主键
builder.HasKey(x => x.Id);
//自增
builder.Property(x => x.Id)
.ValueGeneratedOnAdd();
//外键
builder.HasOne(x => x.User)
.WithMany(u => u.operationLogs)
.HasForeignKey(x => x.UserId);
}
}
}
+28
View File
@@ -0,0 +1,28 @@
using Apimanager_backend.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Apimanager_backend.Data
{
public class OrderConfig : IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
//主键
builder.HasKey(x => x.Id);
//自增
builder.Property(x => x.Id)
.ValueGeneratedOnAdd();
//唯一索引
builder.HasIndex(x => x.OrderNumber)
.IsUnique();
//唯一索引
builder.HasIndex(x => x.ThirdPartyOrderId)
.IsUnique();
//外键
builder.HasOne(x => x.User)
.WithMany(u => u.Orders)
.HasForeignKey(x => x.UserId);
}
}
}
+23
View File
@@ -0,0 +1,23 @@
using Apimanager_backend.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Apimanager_backend.Data
{
public class UserConfig : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
//主键
builder.HasKey(x => x.Id);
//自增
builder.Property(x => x.Id)
.ValueGeneratedOnAdd();
//唯一索引
builder.HasIndex(x => x.Username)
.IsUnique();
builder.HasIndex(x => x.Email)
.IsUnique();
}
}
}
@@ -0,0 +1,25 @@
using Apimanager_backend.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Apimanager_backend.Data
{
public class UserPackageConfig : IEntityTypeConfiguration<UserPackage>
{
public void Configure(EntityTypeBuilder<UserPackage> builder)
{
//主键
builder.HasKey(x => x.Id);
//自增
builder.Property(x => x.Id)
.ValueGeneratedOnAdd();
//外键
builder.HasOne(x => x.User)
.WithMany(u => u.Packages)
.HasForeignKey(x => x.UserId);
builder.HasOne(x => x.Package)
.WithMany(u => u.Packages)
.HasForeignKey(x => x.PackageId);
}
}
}