mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
Code cleanup.
This commit is contained in:
parent
6e5ef1e090
commit
5dd8210653
@ -1,12 +1,15 @@
|
|||||||
using FreeSql;
|
#if NET40
|
||||||
|
using FreeSql.DataAnnotations;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
#else
|
||||||
using FreeSql.DataAnnotations;
|
using FreeSql.DataAnnotations;
|
||||||
using System;
|
using System;
|
||||||
using System.Data;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ReSharper disable once CheckNamespace
|
||||||
namespace FreeSql
|
namespace FreeSql
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -21,9 +24,9 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
static BaseEntity()
|
static BaseEntity()
|
||||||
{
|
{
|
||||||
var tkeyType = typeof(TKey)?.NullableTypeOrThis();
|
var keyType = typeof(TKey).NullableTypeOrThis();
|
||||||
if (tkeyType == typeof(int) || tkeyType == typeof(long))
|
if (keyType == typeof(int) || keyType == typeof(long))
|
||||||
BaseEntity.ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true));
|
ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -32,14 +35,13 @@ namespace FreeSql
|
|||||||
[Column(Position = 1)]
|
[Column(Position = 1)]
|
||||||
public virtual TKey Id { get; set; }
|
public virtual TKey Id { get; set; }
|
||||||
|
|
||||||
#if net40
|
#if !NET40
|
||||||
#else
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据主键值获取数据
|
/// 根据主键值获取数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
async public static Task<TEntity> FindAsync(TKey id)
|
public static async Task<TEntity> FindAsync(TKey id)
|
||||||
{
|
{
|
||||||
var item = await Select.WhereDynamic(id).FirstAsync();
|
var item = await Select.WhereDynamic(id).FirstAsync();
|
||||||
(item as BaseEntity<TEntity>)?.Attach();
|
(item as BaseEntity<TEntity>)?.Attach();
|
||||||
@ -69,15 +71,18 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
bool UpdateIsDeleted(bool value)
|
bool UpdateIsDeleted(bool value)
|
||||||
{
|
{
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
|
{
|
||||||
return Orm.Update<TEntity>(this as TEntity)
|
return Orm.Update<TEntity>(this as TEntity)
|
||||||
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
||||||
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;
|
.Set(a => (a as BaseEntity).IsDeleted, IsDeleted = value).ExecuteAffrows() == 1;
|
||||||
|
}
|
||||||
|
|
||||||
this.IsDeleted = value;
|
IsDeleted = value;
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.Update(this as TEntity) == 1;
|
return Repository.Update(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 删除数据
|
/// 删除数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -85,18 +90,21 @@ namespace FreeSql
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public virtual bool Delete(bool physicalDelete = false)
|
public virtual bool Delete(bool physicalDelete = false)
|
||||||
{
|
{
|
||||||
if (physicalDelete == false) return this.UpdateIsDeleted(true);
|
if (physicalDelete == false)
|
||||||
if (this.Repository == null)
|
return UpdateIsDeleted(true);
|
||||||
|
|
||||||
|
if (Repository is null)
|
||||||
return Orm.Delete<TEntity>(this as TEntity).ExecuteAffrows() == 1;
|
return Orm.Delete<TEntity>(this as TEntity).ExecuteAffrows() == 1;
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.Delete(this as TEntity) == 1;
|
return Repository.Delete(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 恢复删除的数据
|
/// 恢复删除的数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public virtual bool Restore() => this.UpdateIsDeleted(false);
|
public virtual bool Restore() => UpdateIsDeleted(false);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 更新数据
|
/// 更新数据
|
||||||
@ -104,26 +112,29 @@ namespace FreeSql
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public virtual bool Update()
|
public virtual bool Update()
|
||||||
{
|
{
|
||||||
this.UpdateTime = DateTime.Now;
|
UpdateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
|
{
|
||||||
return Orm.Update<TEntity>()
|
return Orm.Update<TEntity>()
|
||||||
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
||||||
.SetSource(this as TEntity).ExecuteAffrows() == 1;
|
.SetSource(this as TEntity).ExecuteAffrows() == 1;
|
||||||
|
}
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.Update(this as TEntity) == 1;
|
return Repository.Update(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 插入数据
|
/// 插入数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual TEntity Insert()
|
public virtual TEntity Insert()
|
||||||
{
|
{
|
||||||
this.CreateTime = DateTime.Now;
|
CreateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.Insert(this as TEntity);
|
return Repository.Insert(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -132,12 +143,12 @@ namespace FreeSql
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public virtual TEntity Save()
|
public virtual TEntity Save()
|
||||||
{
|
{
|
||||||
this.UpdateTime = DateTime.Now;
|
UpdateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.InsertOrUpdate(this as TEntity);
|
return Repository.InsertOrUpdate(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -146,11 +157,11 @@ namespace FreeSql
|
|||||||
/// <param name="navigatePropertyName">导航属性名</param>
|
/// <param name="navigatePropertyName">导航属性名</param>
|
||||||
public virtual void SaveMany(string navigatePropertyName)
|
public virtual void SaveMany(string navigatePropertyName)
|
||||||
{
|
{
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
|
Repository.SaveMany(this as TEntity, navigatePropertyName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,14 @@
|
|||||||
|
#if NET40
|
||||||
using FreeSql;
|
using FreeSql.DataAnnotations;
|
||||||
|
|
||||||
|
#else
|
||||||
using FreeSql.DataAnnotations;
|
using FreeSql.DataAnnotations;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ReSharper disable once CheckNamespace
|
||||||
namespace FreeSql
|
namespace FreeSql
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -18,32 +23,30 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
static BaseEntityAsync()
|
static BaseEntityAsync()
|
||||||
{
|
{
|
||||||
var tkeyType = typeof(TKey)?.NullableTypeOrThis();
|
var keyType = typeof(TKey).NullableTypeOrThis();
|
||||||
if (tkeyType == typeof(int) || tkeyType == typeof(long))
|
if (keyType == typeof(int) || keyType == typeof(long))
|
||||||
BaseEntity.ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true));
|
ConfigEntity(typeof(TEntity), t => t.Property("Id").IsIdentity(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 主键
|
/// 主键
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Column(Position = 1)]
|
[Column(Position = 1)]
|
||||||
public virtual TKey Id { get; set; }
|
public virtual TKey Id { get; set; }
|
||||||
|
|
||||||
#if net40
|
#if !NET40
|
||||||
#else
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据主键值获取数据
|
/// 根据主键值获取数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
async public static Task<TEntity> FindAsync(TKey id)
|
public static async Task<TEntity> FindAsync(TKey id)
|
||||||
{
|
{
|
||||||
var item = await Select.WhereDynamic(id).FirstAsync();
|
var item = await Select.WhereDynamic(id).FirstAsync();
|
||||||
(item as BaseEntity<TEntity>)?.Attach();
|
(item as BaseEntity<TEntity>)?.Attach();
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -53,65 +56,73 @@ namespace FreeSql
|
|||||||
[Table(DisableSyncStructure = true)]
|
[Table(DisableSyncStructure = true)]
|
||||||
public abstract class BaseEntityAsync<TEntity> : BaseEntityReadOnly<TEntity> where TEntity : class
|
public abstract class BaseEntityAsync<TEntity> : BaseEntityReadOnly<TEntity> where TEntity : class
|
||||||
{
|
{
|
||||||
#if net40
|
#if !NET40
|
||||||
#else
|
|
||||||
async Task<bool> UpdateIsDeletedAsync(bool value)
|
async Task<bool> UpdateIsDeletedAsync(bool value)
|
||||||
{
|
{
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
|
{
|
||||||
return await Orm.Update<TEntity>(this as TEntity)
|
return await Orm.Update<TEntity>(this as TEntity)
|
||||||
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
||||||
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1;
|
.Set(a => (a as BaseEntity).IsDeleted, IsDeleted = value).ExecuteAffrowsAsync() == 1;
|
||||||
|
}
|
||||||
|
|
||||||
this.IsDeleted = value;
|
IsDeleted = value;
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
return await Repository.UpdateAsync(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 删除数据
|
/// 删除数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="physicalDelete">是否物理删除</param>
|
/// <param name="physicalDelete">是否物理删除</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
async public virtual Task<bool> DeleteAsync(bool physicalDelete = false)
|
public virtual async Task<bool> DeleteAsync(bool physicalDelete = false)
|
||||||
{
|
{
|
||||||
if (physicalDelete == false) return await this.UpdateIsDeletedAsync(true);
|
if (physicalDelete == false)
|
||||||
if (this.Repository == null)
|
return await UpdateIsDeletedAsync(true);
|
||||||
|
|
||||||
|
if (Repository is null)
|
||||||
return await Orm.Delete<TEntity>(this as TEntity).ExecuteAffrowsAsync() == 1;
|
return await Orm.Delete<TEntity>(this as TEntity).ExecuteAffrowsAsync() == 1;
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return await this.Repository.DeleteAsync(this as TEntity) == 1;
|
return await Repository.DeleteAsync(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 恢复删除的数据
|
/// 恢复删除的数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public virtual Task<bool> RestoreAsync() => this.UpdateIsDeletedAsync(false);
|
public virtual Task<bool> RestoreAsync() => UpdateIsDeletedAsync(false);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 更新数据
|
/// 更新数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
async public virtual Task<bool> UpdateAsync()
|
public virtual async Task<bool> UpdateAsync()
|
||||||
{
|
{
|
||||||
this.UpdateTime = DateTime.Now;
|
UpdateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
|
{
|
||||||
return await Orm.Update<TEntity>()
|
return await Orm.Update<TEntity>()
|
||||||
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
||||||
.SetSource(this as TEntity).ExecuteAffrowsAsync() == 1;
|
.SetSource(this as TEntity).ExecuteAffrowsAsync() == 1;
|
||||||
|
}
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
return await Repository.UpdateAsync(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 插入数据
|
/// 插入数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual Task<TEntity> InsertAsync()
|
public virtual Task<TEntity> InsertAsync()
|
||||||
{
|
{
|
||||||
this.CreateTime = DateTime.Now;
|
CreateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.InsertAsync(this as TEntity);
|
return Repository.InsertAsync(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -120,12 +131,12 @@ namespace FreeSql
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public virtual Task<TEntity> SaveAsync()
|
public virtual Task<TEntity> SaveAsync()
|
||||||
{
|
{
|
||||||
this.UpdateTime = DateTime.Now;
|
UpdateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.InsertOrUpdateAsync(this as TEntity);
|
return Repository.InsertOrUpdateAsync(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -134,12 +145,12 @@ namespace FreeSql
|
|||||||
/// <param name="navigatePropertyName">导航属性名</param>
|
/// <param name="navigatePropertyName">导航属性名</param>
|
||||||
public virtual Task SaveManyAsync(string navigatePropertyName)
|
public virtual Task SaveManyAsync(string navigatePropertyName)
|
||||||
{
|
{
|
||||||
if (this.Repository == null)
|
if (Repository is null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.SaveManyAsync(this as TEntity, navigatePropertyName);
|
return Repository.SaveManyAsync(this as TEntity, navigatePropertyName);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,15 +1,14 @@
|
|||||||
|
using FreeSql.DataAnnotations;
|
||||||
using FreeSql.DataAnnotations;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.Common;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
|
// ReSharper disable once CheckNamespace
|
||||||
namespace FreeSql
|
namespace FreeSql
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -19,6 +18,7 @@ namespace FreeSql
|
|||||||
public abstract class BaseEntity
|
public abstract class BaseEntity
|
||||||
{
|
{
|
||||||
internal static IFreeSql _ormPriv;
|
internal static IFreeSql _ormPriv;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 全局 IFreeSql orm 对象
|
/// 全局 IFreeSql orm 对象
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -26,6 +26,7 @@ namespace FreeSql
|
|||||||
.UseAutoSyncStructure(true)
|
.UseAutoSyncStructure(true)
|
||||||
.UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"")
|
.UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"")
|
||||||
.Build());");
|
.Build());");
|
||||||
|
|
||||||
internal static Func<IUnitOfWork> _resolveUow;
|
internal static Func<IUnitOfWork> _resolveUow;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -52,21 +53,24 @@ namespace FreeSql
|
|||||||
_ormPriv.CodeFirst.ConfigEntity(cei.EntityType, cei.Fluent);
|
_ormPriv.CodeFirst.ConfigEntity(cei.EntityType, cei.Fluent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_resolveUow = resolveUow;
|
_resolveUow = resolveUow;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConfigEntityInfo
|
class ConfigEntityInfo
|
||||||
{
|
{
|
||||||
public Type EntityType;
|
public Type EntityType;
|
||||||
public Action<TableFluent> Fluent;
|
public Action<TableFluent> Fluent;
|
||||||
}
|
}
|
||||||
static ConcurrentQueue<ConfigEntityInfo> _configEntityQueues = new ConcurrentQueue<ConfigEntityInfo>();
|
|
||||||
static object _configEntityLock = new object();
|
static ConcurrentQueue<ConfigEntityInfo> _configEntityQueues = new();
|
||||||
|
static object _configEntityLock = new();
|
||||||
|
|
||||||
internal static void ConfigEntity(Type entityType, Action<TableFluent> fluent)
|
internal static void ConfigEntity(Type entityType, Action<TableFluent> fluent)
|
||||||
{
|
{
|
||||||
lock (_configEntityLock)
|
lock (_configEntityLock)
|
||||||
{
|
{
|
||||||
if (_ormPriv == null)
|
if (_ormPriv is null)
|
||||||
_configEntityQueues.Enqueue(new ConfigEntityInfo { EntityType = entityType, Fluent = fluent });
|
_configEntityQueues.Enqueue(new ConfigEntityInfo { EntityType = entityType, Fluent = fluent });
|
||||||
else
|
else
|
||||||
_ormPriv.CodeFirst.ConfigEntity(entityType, fluent);
|
_ormPriv.CodeFirst.ConfigEntity(entityType, fluent);
|
||||||
@ -78,16 +82,19 @@ namespace FreeSql
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[Column(Position = -4)]
|
[Column(Position = -4)]
|
||||||
public virtual DateTime CreateTime { get; set; } = DateTime.Now;
|
public virtual DateTime CreateTime { get; set; } = DateTime.Now;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 更新时间
|
/// 更新时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Column(Position = -3)]
|
[Column(Position = -3)]
|
||||||
public virtual DateTime UpdateTime { get; set; }
|
public virtual DateTime UpdateTime { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 逻辑删除
|
/// 逻辑删除
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Column(Position = -2)]
|
[Column(Position = -2)]
|
||||||
public virtual bool IsDeleted { get; set; }
|
public virtual bool IsDeleted { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 排序
|
/// 排序
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -107,25 +114,29 @@ namespace FreeSql
|
|||||||
get
|
get
|
||||||
{
|
{
|
||||||
var select = Orm.Select<TEntity>()
|
var select = Orm.Select<TEntity>()
|
||||||
.TrackToList(TrackToList) //自动为每个元素 Attach
|
.TrackToList(TrackToList) //自动为每个元素 Attach
|
||||||
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction(false));
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction(false));
|
||||||
return select.WhereCascade(a => (a as BaseEntity).IsDeleted == false);
|
return select.WhereCascade(a => (a as BaseEntity).IsDeleted == false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TrackToList(object list)
|
static void TrackToList(object list)
|
||||||
{
|
{
|
||||||
if (list == null) return;
|
if (list is null)
|
||||||
var ls = list as IList<TEntity>;
|
return;
|
||||||
if (ls == null)
|
|
||||||
|
if (list is not IList<TEntity> ls)
|
||||||
{
|
{
|
||||||
var ie = list as IEnumerable;
|
if (list is not IEnumerable ie)
|
||||||
if (ie == null) return;
|
return;
|
||||||
|
|
||||||
var isFirst = true;
|
var isFirst = true;
|
||||||
|
|
||||||
IBaseRepository<TEntity> berepo = null;
|
IBaseRepository<TEntity> berepo = null;
|
||||||
|
|
||||||
foreach (var item in ie)
|
foreach (var item in ie)
|
||||||
{
|
{
|
||||||
if (item == null) return;
|
if (item is null) return;
|
||||||
if (isFirst)
|
if (isFirst)
|
||||||
{
|
{
|
||||||
isFirst = false;
|
isFirst = false;
|
||||||
@ -135,28 +146,36 @@ namespace FreeSql
|
|||||||
if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return;
|
if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return;
|
||||||
if (item is BaseEntity<TEntity> == false) return;
|
if (item is BaseEntity<TEntity> == false) return;
|
||||||
}
|
}
|
||||||
var beitem = item as BaseEntity<TEntity>;
|
|
||||||
if (beitem != null)
|
if (item is BaseEntity<TEntity> entity)
|
||||||
{
|
{
|
||||||
if (berepo == null) berepo = Orm.GetRepository<TEntity>();
|
berepo ??= Orm.GetRepository<TEntity>();
|
||||||
beitem.Repository = berepo;
|
entity.Repository = berepo;
|
||||||
beitem.Attach();
|
entity.Attach();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (ls.Any() == false) return;
|
|
||||||
if (ls.FirstOrDefault() is BaseEntity<TEntity> == false) return;
|
if (ls.Any() == false)
|
||||||
if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) return;
|
return;
|
||||||
|
|
||||||
|
if (ls.FirstOrDefault() is not BaseEntity<TEntity>)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true)
|
||||||
|
return;
|
||||||
|
|
||||||
IBaseRepository<TEntity> repo = null;
|
IBaseRepository<TEntity> repo = null;
|
||||||
|
|
||||||
foreach (var item in ls)
|
foreach (var item in ls)
|
||||||
{
|
{
|
||||||
var beitem = item as BaseEntity<TEntity>;
|
if (item is BaseEntity<TEntity> entity)
|
||||||
if (beitem != null)
|
|
||||||
{
|
{
|
||||||
if (repo == null) repo = Orm.GetRepository<TEntity>();
|
repo ??= Orm.GetRepository<TEntity>();
|
||||||
beitem.Repository = repo;
|
entity.Repository = repo;
|
||||||
beitem.Attach();
|
entity.Attach();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -167,6 +186,7 @@ namespace FreeSql
|
|||||||
/// <param name="exp">lambda表达式</param>
|
/// <param name="exp">lambda表达式</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
|
public static ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查询条件,Where(true, a => a.Id > 10),支导航对象查询,Where(true, a => a.Author.Email == "2881099@qq.com")
|
/// 查询条件,Where(true, a => a.Id > 10),支导航对象查询,Where(true, a => a.Author.Email == "2881099@qq.com")
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -185,12 +205,10 @@ namespace FreeSql
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public TEntity Attach()
|
public TEntity Attach()
|
||||||
{
|
{
|
||||||
if (this.Repository == null)
|
Repository ??= Orm.GetRepository<TEntity>();
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
|
||||||
|
|
||||||
var item = this as TEntity;
|
var item = this as TEntity;
|
||||||
this.Repository.Attach(item);
|
Repository.Attach(item);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,8 +6,8 @@
|
|||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Authors>FreeSql;ncc;YeXiangQin</Authors>
|
<Authors>FreeSql;ncc;YeXiangQin</Authors>
|
||||||
<Description>BaseEntity 是一种极简单的 CodeFirst 开发方式,特别对单表或多表CRUD,利用继承节省了每个实体类的重复属性(创建时间、ID等字段),软件删除等功能,进行 crud 操作时不必时常考虑仓储的使用.</Description>
|
<Description>BaseEntity 是一种极简单的 CodeFirst 开发方式,特别对单表或多表CRUD,利用继承节省了每个实体类的重复属性(创建时间、ID等字段),软件删除等功能,进行 crud 操作时不必时常考虑仓储的使用.</Description>
|
||||||
<PackageProjectUrl>https://github.com/2881099/FreeSql/tree/master/Extensions/FreeSql.Extensions.BaseEntity</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/dotnetcore/FreeSql/tree/master/Extensions/FreeSql.Extensions.BaseEntity</PackageProjectUrl>
|
||||||
<RepositoryUrl>https://github.com/2881099/FreeSql/tree/master/Extensions/FreeSql.Extensions.BaseEntity</RepositoryUrl>
|
<RepositoryUrl>https://github.com/dotnetcore/FreeSql/tree/master/Extensions/FreeSql.Extensions.BaseEntity</RepositoryUrl>
|
||||||
<RepositoryType>git</RepositoryType>
|
<RepositoryType>git</RepositoryType>
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<PackageTags>FreeSql;ORM;BaseEntity</PackageTags>
|
<PackageTags>FreeSql;ORM;BaseEntity</PackageTags>
|
||||||
@ -19,6 +19,7 @@
|
|||||||
<SignAssembly>true</SignAssembly>
|
<SignAssembly>true</SignAssembly>
|
||||||
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
|
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
|
||||||
<DelaySign>false</DelaySign>
|
<DelaySign>false</DelaySign>
|
||||||
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user