webapi 集成 之 freesql 注入


复制代码
using EasyCaching.SQLite;
using jxc.Repository;
using jxc.Service;

namespace jxc.Api;

public class Program
{
    public static void Main(string[] args)
    {
        WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

        // Add services to the container.
        builder.Services.AddControllers();
        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();

        IServiceCollection services = builder.Services;

        Func<IServiceProvider, IFreeSql> fsqlFactory = r =>
        {
            IFreeSql fsql = new FreeSql.FreeSqlBuilder()
                .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=freedb2024.db")
                .UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}"))
                .UseAutoSyncStructure(true) //自动同步实体结构到数据库,只有CRUD时才会生成表
                .Build();
            return fsql;
        };
        services.AddSingleton<IFreeSql>(fsqlFactory);

        services.AddSingleton<IInventoryRepository, InventoryRepository>();
        services.AddScoped<InventoryService>();
        var app = builder.Build();
        // Configure the HTTP request pipeline.
        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }
        app.UseAuthorization();
        app.MapControllers();
        app.Run();
    }
}
复制代码
posted @   网络来者  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示