mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
源代码改用vs默认格式化
This commit is contained in:
@ -3,30 +3,35 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace FreeSql {
|
||||
public static class DbContextDependencyInjection {
|
||||
namespace FreeSql
|
||||
{
|
||||
public static class DbContextDependencyInjection
|
||||
{
|
||||
|
||||
public static IServiceCollection AddFreeDbContext<TDbContext>(this IServiceCollection services, Action<DbContextOptionsBuilder> options) where TDbContext : DbContext {
|
||||
public static IServiceCollection AddFreeDbContext<TDbContext>(this IServiceCollection services, Action<DbContextOptionsBuilder> options) where TDbContext : DbContext
|
||||
{
|
||||
|
||||
services.AddScoped<TDbContext>(sp => {
|
||||
var ctx = Activator.CreateInstance<TDbContext>();
|
||||
services.AddScoped<TDbContext>(sp =>
|
||||
{
|
||||
var ctx = Activator.CreateInstance<TDbContext>();
|
||||
|
||||
if (ctx._orm == null) {
|
||||
var builder = new DbContextOptionsBuilder();
|
||||
options(builder);
|
||||
ctx._orm = builder._fsql;
|
||||
if (ctx._orm == null)
|
||||
{
|
||||
var builder = new DbContextOptionsBuilder();
|
||||
options(builder);
|
||||
ctx._orm = builder._fsql;
|
||||
|
||||
if (ctx._orm == null)
|
||||
throw new Exception("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
||||
if (ctx._orm == null)
|
||||
throw new Exception("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
||||
|
||||
ctx.InitPropSets();
|
||||
}
|
||||
ctx.InitPropSets();
|
||||
}
|
||||
|
||||
return ctx;
|
||||
});
|
||||
return ctx;
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@ -2,37 +2,41 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
public static class FreeSqlDbContextExtenssions {
|
||||
public static class FreeSqlDbContextExtenssions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 创建普通数据上下文档对象
|
||||
/// </summary>
|
||||
/// <param name="that"></param>
|
||||
/// <returns></returns>
|
||||
public static DbContext CreateDbContext(this IFreeSql that) {
|
||||
return new FreeContext(that);
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建普通数据上下文档对象
|
||||
/// </summary>
|
||||
/// <param name="that"></param>
|
||||
/// <returns></returns>
|
||||
public static DbContext CreateDbContext(this IFreeSql that)
|
||||
{
|
||||
return new FreeContext(that);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不跟踪查询的实体数据(在不需要更新其数据时使用),可提长查询性能
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="select"></param>
|
||||
/// <returns></returns>
|
||||
public static ISelect<T> NoTracking<T>(this ISelect<T> select) where T : class {
|
||||
return select.TrackToList(null);
|
||||
}
|
||||
/// <summary>
|
||||
/// 不跟踪查询的实体数据(在不需要更新其数据时使用),可提长查询性能
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="select"></param>
|
||||
/// <returns></returns>
|
||||
public static ISelect<T> NoTracking<T>(this ISelect<T> select) where T : class
|
||||
{
|
||||
return select.TrackToList(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置 DbContext 选项设置
|
||||
/// </summary>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="options"></param>
|
||||
public static void SetDbContextOptions(this IFreeSql that, Action<DbContextOptions> options) {
|
||||
if (options == null) return;
|
||||
var cfg = _dicSetDbContextOptions.GetOrAdd(that, t => new DbContextOptions());
|
||||
options(cfg);
|
||||
_dicSetDbContextOptions.AddOrUpdate(that, cfg, (t, o) => cfg);
|
||||
}
|
||||
internal static ConcurrentDictionary<IFreeSql, DbContextOptions> _dicSetDbContextOptions = new ConcurrentDictionary<IFreeSql, DbContextOptions>();
|
||||
/// <summary>
|
||||
/// 设置 DbContext 选项设置
|
||||
/// </summary>
|
||||
/// <param name="that"></param>
|
||||
/// <param name="options"></param>
|
||||
public static void SetDbContextOptions(this IFreeSql that, Action<DbContextOptions> options)
|
||||
{
|
||||
if (options == null) return;
|
||||
var cfg = _dicSetDbContextOptions.GetOrAdd(that, t => new DbContextOptions());
|
||||
options(cfg);
|
||||
_dicSetDbContextOptions.AddOrUpdate(that, cfg, (t, o) => cfg);
|
||||
}
|
||||
internal static ConcurrentDictionary<IFreeSql, DbContextOptions> _dicSetDbContextOptions = new ConcurrentDictionary<IFreeSql, DbContextOptions>();
|
||||
}
|
Reference in New Issue
Block a user