添加ORM的依赖注入,添加appsettings的gitignore
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Model;
|
||||
using Model.Entity.Asset;
|
||||
using ORM;
|
||||
using Service.Implement;
|
||||
using Service.Interface;
|
||||
using SqlSugar;
|
||||
using System.Collections.Generic;
|
||||
@@ -20,12 +18,9 @@ namespace IOT_API.Controllers.Asset
|
||||
{
|
||||
private readonly IEquipmentService _equipmentService;
|
||||
|
||||
public EquipmentController()
|
||||
public EquipmentController(IEquipmentService equipmentService)
|
||||
{
|
||||
// 项目未注册 DI 容器,SqlSugarContext.DbContext 为静态单例,直接实例化服务
|
||||
_equipmentService = new EquipmentService(
|
||||
new SqlSugarRepository<EquipmentEntity>(),
|
||||
new SqlSugarRepository<EquipmentStatusRecordEntity>());
|
||||
_equipmentService = equipmentService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using ORM;
|
||||
using Service.Implement;
|
||||
using SqlSugar;
|
||||
using System.Reflection;
|
||||
|
||||
namespace WebAPI
|
||||
@@ -15,6 +17,12 @@ namespace WebAPI
|
||||
/// </summary>
|
||||
public static IServiceCollection AddBusinessServices(this IServiceCollection services)
|
||||
{
|
||||
// SqlSugar 客户端:静态单例(SqlSugarScope 线程安全),供仓储及其他需要注入 ISqlSugarClient 的地方使用
|
||||
services.AddSingleton<ISqlSugarClient>(SqlSugarContext.DbContext);
|
||||
|
||||
// 泛型仓储:所有 SqlSugarRepository<TEntity> 统一注册(内部使用 SqlSugarContext.DbContext 静态单例)
|
||||
services.AddScoped(typeof(SqlSugarRepository<>));
|
||||
|
||||
// Service 项目程序集(接口与实现都在同一个程序集里)
|
||||
Assembly assembly = typeof(BaseService<>).Assembly;
|
||||
|
||||
|
||||
@@ -48,27 +48,6 @@ namespace WebAPI
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// ======= Database init start =======
|
||||
// Npgsql 时间兼容开关:允许向 PostgreSQL 写入本地时间 DateTime(DateTime.Now)
|
||||
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
||||
|
||||
// 从 appsettings.json 读取数据库配置(PostgreSQL)
|
||||
var dbConfig = builder.Configuration.GetSection("Database");
|
||||
DatabaseConfig.InitPostgreSql(
|
||||
dbConfig["Server"] ?? "localhost",
|
||||
int.TryParse(dbConfig["Port"], out var port) ? port : 5432,
|
||||
dbConfig["Database"] ?? "iot",
|
||||
dbConfig["Uid"] ?? "postgres",
|
||||
dbConfig["Pwd"] ?? "");
|
||||
|
||||
// 数据库不存在则创建,并检查连接(租户雪花机器码可按需设置)
|
||||
DatabaseConfig.SetTenant(10001);
|
||||
DatabaseConfig.CreateDatabaseAndCheckConnection(createDatabase: true, checkConnection: true);
|
||||
|
||||
// CodeFirst 自动建表:扫描 Model.dll 中所有以 Entity 结尾的类建表/补列
|
||||
SqlSugarContext.InitDatabase();
|
||||
// ======= Database init end =======
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
|
||||
Reference in New Issue
Block a user