新增DbSet和SugarDbTables定义。可以继承SugarDbTables并使用DbSet定义表
This commit is contained in:
parent
5fa1a78ddf
commit
bfe7ecd81a
25
Falcon.SugarApi/DatabaseDefinitions/DbSet.cs
Normal file
25
Falcon.SugarApi/DatabaseDefinitions/DbSet.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using SqlSugar;
|
||||
|
||||
namespace Falcon.SugarApi.DatabaseDefinitions
|
||||
{
|
||||
/// <summary>
|
||||
/// 代表一张表
|
||||
/// </summary>
|
||||
/// <typeparam name="T">表定义类型</typeparam>
|
||||
public class DbSet<T> : SimpleClient<T> where T : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过数据上下文构造一张表
|
||||
/// </summary>
|
||||
/// <param name="context">数据上下文</param>
|
||||
public DbSet(SqlSugarClient context) : base(context) { }
|
||||
/// <summary>
|
||||
/// 返回可迭代的
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual ISugarQueryable<T> Queryable() {
|
||||
return Context.Queryable<T>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -15,7 +15,9 @@ namespace Falcon.SugarApi.DatabaseDefinitions
|
|||
/// 是否使用log
|
||||
/// </summary>
|
||||
public bool Log { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 实例化SugarDb链接配置
|
||||
/// </summary>
|
||||
public SugarConnectionConfig() {
|
||||
this.ConfigureExternalServices ??= new ConfigureExternalServices { };
|
||||
this.ConfigureExternalServices.EntityService += (p, c) => {
|
35
Falcon.SugarApi/DatabaseDefinitions/SugarDbTables.cs
Normal file
35
Falcon.SugarApi/DatabaseDefinitions/SugarDbTables.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
using SqlSugar;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Falcon.SugarApi.DatabaseDefinitions
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示数据库表结构定义
|
||||
/// </summary>
|
||||
public abstract class SugarDbTables
|
||||
{
|
||||
/// <summary>
|
||||
/// 表使用的数据上下文
|
||||
/// </summary>
|
||||
private SugarDbContext DbContext { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 通过数据上下文构造表集合对象,并实例化所有DbSet实例
|
||||
/// </summary>
|
||||
/// <param name="dbContext">使用的数据上下文</param>
|
||||
/// <param name="createInstance">是否实例化所有DbSet表对象</param>
|
||||
public SugarDbTables(SugarDbContext dbContext, bool createInstance = true) {
|
||||
this.DbContext = dbContext;
|
||||
if (createInstance) {
|
||||
foreach (PropertyInfo property in this.GetType().GetProperties()) {
|
||||
var ptype = property.PropertyType;
|
||||
if (ptype.IsGenericType() && ptype.GetGenericTypeDefinition() == typeof(DbSet<>) && property.CanWrite) {
|
||||
property.SetValue(this, Activator.CreateInstance(ptype, this.DbContext));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -14,16 +14,6 @@ namespace Falcon.SugarApi
|
|||
/// </summary>
|
||||
public static class IServiceCollectionExtend
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册Falcon.Sugar相关api方法和数据库上下文。
|
||||
/// </summary>
|
||||
/// <param name="services">服务集合</param>
|
||||
/// <param name="config">数据上下文配置节点</param>
|
||||
/// <returns>服务集合</returns>
|
||||
public static IServiceCollection AddSugarApiWithDbContext(this IServiceCollection services, SugarConnectionConfig config) {
|
||||
return services.AddSugarApiDbContext(config).AddApiReturnModelProvider();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册sugarDbcontext数据上下文
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue
Block a user