Files
WorkStation/WorkStation.Server/Program.cs

54 lines
1.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();