mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
更新错误的命名 #103
This commit is contained in:
46
FreeSql.DbContext/Extensions/DependencyInjection.cs
Normal file
46
FreeSql.DbContext/Extensions/DependencyInjection.cs
Normal file
@ -0,0 +1,46 @@
|
||||
#if ns20
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
public static class DbContextDependencyInjection
|
||||
{
|
||||
static IServiceCollection AddFreeDbContext(this IServiceCollection services, Type dbContextType, Action<DbContextOptionsBuilder> options)
|
||||
{
|
||||
services.AddScoped(dbContextType, sp =>
|
||||
{
|
||||
var ctx = Activator.CreateInstance(dbContextType) as DbContext;
|
||||
if (ctx != null && ctx._ormPriv == null)
|
||||
{
|
||||
var builder = new DbContextOptionsBuilder();
|
||||
options(builder);
|
||||
ctx._ormPriv = builder._fsql;
|
||||
ctx._optionsPriv = builder._options;
|
||||
|
||||
if (ctx._ormPriv == null)
|
||||
throw new Exception("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
||||
|
||||
ctx.InitPropSets();
|
||||
}
|
||||
return ctx;
|
||||
});
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddFreeDbContext<TDbContext>(this IServiceCollection services, Action<DbContextOptionsBuilder> options) where TDbContext : DbContext =>
|
||||
AddFreeDbContext(services, typeof(TDbContext), options);
|
||||
|
||||
public static IServiceCollection AddFreeDbContext(this IServiceCollection services, Action<DbContextOptionsBuilder> options, Assembly[] assemblies)
|
||||
{
|
||||
if (assemblies?.Any() == true)
|
||||
foreach (var asse in assemblies)
|
||||
foreach (var dbType in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(DbContext).IsAssignableFrom(a)))
|
||||
AddFreeDbContext(services, dbType, options);
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
42
FreeSql.DbContext/Extensions/FreeSqlDbContextExtensions.cs
Normal file
42
FreeSql.DbContext/Extensions/FreeSqlDbContextExtensions.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using FreeSql;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
public static class FreeSqlDbContextExtensions
|
||||
{
|
||||
|
||||
/// <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>
|
||||
/// 设置 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