54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
using Arch.EntityFrameworkCore.UnitOfWork;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using WorkStation.Server.Context;
|
||
using WorkStation.Server.Context.Entity;
|
||
using WorkStation.Server.Context.Repository;
|
||
using WorkStation.Server.Services;
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
|
||
// Add services to the container.
|
||
|
||
// 获取配置文件中的MySQL数据库连接字符串
|
||
var connectionString = builder.Configuration.GetConnectionString("MySQLConnection");
|
||
|
||
// 配置MyDbContext使用的数据库上下文选项
|
||
builder.Services.AddDbContext<MyDbContext>(options =>
|
||
{
|
||
// 使用MySQL数据库,并自动检测服务器版本
|
||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
|
||
});
|
||
|
||
// 添加单元OfWork管理器,用于处理事务和数据库操作的提交
|
||
builder.Services.AddUnitOfWork<MyDbContext>();
|
||
|
||
// 添加自定义仓库,用于处理User实体的数据库操作
|
||
builder.Services.AddCustomRepository<User, UserRepository>();
|
||
|
||
// 注册UserService为瞬态服务,每次请求都会创建一个新的实例
|
||
builder.Services.AddTransient<IUserService, UserService>();
|
||
|
||
builder.Services.AddControllers();
|
||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||
builder.Services.AddEndpointsApiExplorer();
|
||
builder.Services.AddSwaggerGen();
|
||
|
||
var app = builder.Build();
|
||
|
||
|
||
// Configure the HTTP request pipeline.
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
app.UseSwagger();
|
||
app.UseSwaggerUI();
|
||
}
|
||
|
||
app.UseHttpsRedirection();
|
||
|
||
app.UseAuthorization();
|
||
|
||
app.MapControllers();
|
||
|
||
app.Run();
|