initial commit

This commit is contained in:
tk
2024-11-13 18:18:28 +08:00
commit 013f35e296
1500 changed files with 443723 additions and 0 deletions

View File

@ -0,0 +1,56 @@
#if netcoreapp
using FreeSql;
using System;
using System.Linq;
using System.Reflection;
namespace Microsoft.Extensions.DependencyInjection
{
public static class FreeSqlDbContextDependencyInjection
{
static IServiceCollection AddFreeDbContext(this IServiceCollection services, Type dbContextType, Action<DbContextOptionsBuilder> options)
{
services.AddScoped(dbContextType, sp =>
{
DbContext ctx = null;
try
{
var ctor = dbContextType. GetConstructors().FirstOrDefault();
var ctorParams = ctor.GetParameters().Select(a => sp.GetService(a.ParameterType)).ToArray();
ctx = Activator.CreateInstance(dbContextType, ctorParams) as DbContext;
}
catch(Exception ex)
{
throw new Exception(DbContextStrings.AddFreeDbContextError_CheckConstruction(dbContextType.Name), ex);
}
if (ctx != null && ctx._ormScoped == null)
{
var builder = new DbContextOptionsBuilder();
options(builder);
ctx._ormScoped = DbContextScopedFreeSql.Create(builder._fsql, () => ctx, () => ctx.UnitOfWork);
ctx._optionsPriv = builder._options;
if (ctx._ormScoped == null)
throw new Exception(DbContextStrings.ConfigureUseFreeSql);
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

View File

@ -0,0 +1,44 @@
using FreeSql;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
public static partial 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 IFreeSql SetDbContextOptions(this IFreeSql that, Action<DbContextOptions> options)
{
if (options == null) return that;
var cfg = _dicSetDbContextOptions.GetOrAdd(that.Ado.Identifier, t => new DbContextOptions());
options(cfg);
_dicSetDbContextOptions.AddOrUpdate(that.Ado.Identifier, cfg, (t, o) => cfg);
return that;
}
internal static ConcurrentDictionary<Guid, DbContextOptions> _dicSetDbContextOptions = new ConcurrentDictionary<Guid, DbContextOptions>();
}