# volo.abp.freesql **Repository Path**: jonvon/volo.abp.freesql ## Basic Information - **Project Name**: volo.abp.freesql - **Description**: No description available - **Primary Language**: C# - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-09-02 - **Last Updated**: 2024-09-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # volo.abp.freesql - FreeSql是一款优秀的Orm框架,首先感谢FreeSql的作者, Abp VNext也是一个很优秀的后台框架 引入Volo.Abp.FreeSql 作为扩展模块,集成到Abp.VNext中.方便应用. - 使用者所做任何事情也与本作者无关。觉得好用请给个星 - FreeSql与Abp Venxt的设计模式不一致,导致FreeSql某些功能不能在Abp Venxt应用 - 须知: - 每个 SqlConnection GetFreeSql() 返回的 IFreeSql 实例相同;即 同一个TDbContext类型 的多个DbContext实例 返回的 IFreeSql 实例相同 - 可以对 fsql 设置 Aop 事件,比如监视 SQL; - IFreeSql 的ICodeFirst 不可用 - IFreeSql 自身的成员 IDbFirst、Transaction 不可用; ## 安装 > dotnet add package AbpVNext.FreeSql ## 使用 ```csharp ///在一个模块引入Volo.Abp.FreeSql public class TestModel { [Column(IsIdentity =true,IsPrimary =true)] public int Id { get; set; } public string a { get; set; } public int b { get; set; } } /// /// 定义Ef的DbContext 继承FreeSqlEfDbContext /// [ConnectionStringName("Default")] public class TestDbContext : FreeSqlEfDbContext { public TestDbContext(DbContextOptions options) : base(options) { } } /// /// 定义仓储接口 继承 IFreeSqlRepository 有Freesql的增删改查功能 /// public interface ITestRepository : IFreeSqlRepository { } /// /// 接口实现,继承FreeSqlRepository抽象类,默认实现了FreeSql的增删改查功能 /// public class TestRepository : FreeSqlRepository, ITestRepository { public TestRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) { } } /// /// 使用仓储示例 /// public class HelloWorldService : ITransientDependency { private ITestRepository TestRepository; private IUnitOfWorkManager UnitOfWorkManager; public HelloWorldService(IUnitOfWorkManager unitOfWorkManager,ITestRepository testRepository) { TestRepository = testRepository; UnitOfWorkManager = unitOfWorkManager; } /// /// 使用仓储 /// /// public async Task FreeSqlTestAsync() { using (var uow = UnitOfWorkManager.Begin(new AbpUnitOfWorkOptions { IsTransactional = true, IsolationLevel = System.Data.IsolationLevel.Serializable })) { var list = await TestRepository.Select().ToListAsync(); } } public void SayHello() { Console.WriteLine("Hello World!"); } } /// /// 引入依赖AbpFreeSqlModule (AbpAutofacModule只是主程序运行该模块需要注入) /// [DependsOn( typeof(AbpAutofacModule), typeof(AbpFreeSqlModule) )] public class freesqlModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { ///无关代码,只是在这个模块当作运行模块而已 var configuration = context.Services.GetConfiguration(); var hostEnvironment = context.Services.GetSingletonInstance(); ////添加TestDbContext注册到服务 context.Services.AddAbpDbContext(options => { ///取消EF通用仓储绑定到TestDbContext options.AddDefaultRepositories(false); }); ///DbContext配置 Configure(option => { ///默认TestDbContext用SqlServer option.UseSqlServer(); }); ///Freesql配置 Configure(opt => { ///TestDbContext对应的Freesql初始化Action ///注意Action会放到AbpFreeSqlOption列表上,因此多次 ConfigureFreeSql配置是追加Action,不是覆盖原有的Action opt.ConfigureFreeSql((freesql => { freesql.Aop.CommandBefore += (_, e) => Console.WriteLine(e.Command.CommandText); })); }); ///无关代码,只是在这个模块当作运行模块需要而已 context.Services.AddHostedService(); } } ```