mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
- 调整 BaseEntity,移除 BaseTreeEntity、Tenant 租户,改变事务习惯;
This commit is contained in:
parent
fc828d15a6
commit
6d5575def1
@ -37,6 +37,8 @@ namespace base_entity
|
|||||||
public string title { get; set; }
|
public string title { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static AsyncLocal<IUnitOfWork> _asyncUow = new AsyncLocal<IUnitOfWork>();
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -73,7 +75,7 @@ namespace base_entity
|
|||||||
.UseMonitorCommand(cmd => Console.Write(cmd.CommandText))
|
.UseMonitorCommand(cmd => Console.Write(cmd.CommandText))
|
||||||
.UseLazyLoading(true)
|
.UseLazyLoading(true)
|
||||||
.Build();
|
.Build();
|
||||||
BaseEntity.Initialization(fsql);
|
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
var test01 = EMSServerModel.Model.User.Select.IncludeMany(a => a.Roles).ToList();
|
var test01 = EMSServerModel.Model.User.Select.IncludeMany(a => a.Roles).ToList();
|
||||||
@ -144,9 +146,17 @@ namespace base_entity
|
|||||||
|
|
||||||
Task.Run(async () =>
|
Task.Run(async () =>
|
||||||
{
|
{
|
||||||
using (var uow = BaseEntity.Begin())
|
using (var uow = BaseEntity.Orm.CreateUnitOfWork())
|
||||||
{
|
{
|
||||||
var id = (await new User1().SaveAsync()).Id;
|
_asyncUow.Value = uow;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var id = (await new User1().SaveAsync()).Id;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_asyncUow.Value = null;
|
||||||
|
}
|
||||||
uow.Commit();
|
uow.Commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
#if netcore
|
using FreeSql;
|
||||||
|
|
||||||
using FreeSql;
|
|
||||||
using FreeSql.DataAnnotations;
|
using FreeSql.DataAnnotations;
|
||||||
using System;
|
using System;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
@ -34,6 +32,8 @@ namespace FreeSql
|
|||||||
[Column(Position = 1)]
|
[Column(Position = 1)]
|
||||||
public virtual TKey Id { get; set; }
|
public virtual TKey Id { get; set; }
|
||||||
|
|
||||||
|
#if net40
|
||||||
|
#else
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据主键值获取数据
|
/// 根据主键值获取数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -45,6 +45,7 @@ namespace FreeSql
|
|||||||
(item as BaseEntity<TEntity>)?.Attach();
|
(item as BaseEntity<TEntity>)?.Attach();
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据主键值获取数据
|
/// 根据主键值获取数据
|
||||||
@ -70,12 +71,11 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return Orm.Update<TEntity>(this as TEntity)
|
return Orm.Update<TEntity>(this as TEntity)
|
||||||
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
||||||
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;
|
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;
|
||||||
|
|
||||||
this.SetTenantId();
|
|
||||||
this.IsDeleted = value;
|
this.IsDeleted = value;
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.Update(this as TEntity) == 1;
|
return this.Repository.Update(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -88,8 +88,8 @@ namespace FreeSql
|
|||||||
if (physicalDelete == false) return this.UpdateIsDeleted(true);
|
if (physicalDelete == false) return this.UpdateIsDeleted(true);
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return Orm.Delete<TEntity>(this as TEntity).ExecuteAffrows() == 1;
|
return Orm.Delete<TEntity>(this as TEntity).ExecuteAffrows() == 1;
|
||||||
//this.SetTenantId();
|
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.Delete(this as TEntity) == 1;
|
return this.Repository.Delete(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -107,11 +107,10 @@ namespace FreeSql
|
|||||||
this.UpdateTime = DateTime.Now;
|
this.UpdateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return Orm.Update<TEntity>()
|
return Orm.Update<TEntity>()
|
||||||
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
||||||
.SetSource(this as TEntity).ExecuteAffrows() == 1;
|
.SetSource(this as TEntity).ExecuteAffrows() == 1;
|
||||||
|
|
||||||
this.SetTenantId();
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
|
||||||
return this.Repository.Update(this as TEntity) == 1;
|
return this.Repository.Update(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -123,8 +122,7 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.SetTenantId();
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
|
||||||
return this.Repository.Insert(this as TEntity);
|
return this.Repository.Insert(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,8 +136,7 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.SetTenantId();
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
|
||||||
return this.Repository.InsertOrUpdate(this as TEntity);
|
return this.Repository.InsertOrUpdate(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,10 +149,8 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
|
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
@ -1,5 +1,4 @@
|
|||||||
#if netcore
|
|
||||||
|
|
||||||
using FreeSql;
|
using FreeSql;
|
||||||
using FreeSql.DataAnnotations;
|
using FreeSql.DataAnnotations;
|
||||||
using System;
|
using System;
|
||||||
@ -27,8 +26,11 @@ namespace FreeSql
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 主键
|
/// 主键
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[Column(Position = 1)]
|
||||||
public virtual TKey Id { get; set; }
|
public virtual TKey Id { get; set; }
|
||||||
|
|
||||||
|
#if net40
|
||||||
|
#else
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 根据主键值获取数据
|
/// 根据主键值获取数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -40,6 +42,8 @@ namespace FreeSql
|
|||||||
(item as BaseEntity<TEntity>)?.Attach();
|
(item as BaseEntity<TEntity>)?.Attach();
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -49,15 +53,17 @@ 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
|
||||||
|
#else
|
||||||
async Task<bool> UpdateIsDeletedAsync(bool value)
|
async Task<bool> UpdateIsDeletedAsync(bool value)
|
||||||
{
|
{
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return await Orm.Update<TEntity>(this as TEntity)
|
return await Orm.Update<TEntity>(this as TEntity)
|
||||||
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
||||||
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1;
|
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1;
|
||||||
|
|
||||||
this.IsDeleted = value;
|
this.IsDeleted = value;
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -70,8 +76,8 @@ namespace FreeSql
|
|||||||
if (physicalDelete == false) return await this.UpdateIsDeletedAsync(true);
|
if (physicalDelete == false) return await this.UpdateIsDeletedAsync(true);
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return await Orm.Delete<TEntity>(this as TEntity).ExecuteAffrowsAsync() == 1;
|
return await Orm.Delete<TEntity>(this as TEntity).ExecuteAffrowsAsync() == 1;
|
||||||
//this.SetTenantId();
|
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return await this.Repository.DeleteAsync(this as TEntity) == 1;
|
return await this.Repository.DeleteAsync(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -89,11 +95,10 @@ namespace FreeSql
|
|||||||
this.UpdateTime = DateTime.Now;
|
this.UpdateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return await Orm.Update<TEntity>()
|
return await Orm.Update<TEntity>()
|
||||||
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction())
|
||||||
.SetSource(this as TEntity).ExecuteAffrowsAsync() == 1;
|
.SetSource(this as TEntity).ExecuteAffrowsAsync() == 1;
|
||||||
|
|
||||||
this.SetTenantId();
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
|
||||||
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -105,8 +110,7 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.SetTenantId();
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
|
||||||
return this.Repository.InsertAsync(this as TEntity);
|
return this.Repository.InsertAsync(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,8 +124,7 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.SetTenantId();
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
|
||||||
return this.Repository.InsertOrUpdateAsync(this as TEntity);
|
return this.Repository.InsertOrUpdateAsync(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,10 +137,9 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
this.Repository.UnitOfWork = _resolveUow?.Invoke();
|
||||||
return this.Repository.SaveManyAsync(this as TEntity, navigatePropertyName);
|
return this.Repository.SaveManyAsync(this as TEntity, navigatePropertyName);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
@ -1,11 +1,10 @@
|
|||||||
#if netcore
|
|
||||||
|
|
||||||
using FreeSql.DataAnnotations;
|
using FreeSql.DataAnnotations;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Data;
|
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;
|
||||||
@ -27,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;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化BaseEntity
|
/// 初始化BaseEntity
|
||||||
@ -39,7 +39,8 @@ namespace FreeSql
|
|||||||
/// .Build());
|
/// .Build());
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fsql">IFreeSql orm 对象</param>
|
/// <param name="fsql">IFreeSql orm 对象</param>
|
||||||
public static void Initialization(IFreeSql fsql)
|
/// <param name="resolveUow">工作单元(事务)委托,如果不使用事务请传 null<para></para>解释:由于AsyncLocal平台兼容不好,所以交给外部管理</param>
|
||||||
|
public static void Initialization(IFreeSql fsql, Func<IUnitOfWork> resolveUow)
|
||||||
{
|
{
|
||||||
_ormPriv = fsql;
|
_ormPriv = fsql;
|
||||||
_ormPriv.Aop.CurdBefore += (s, e) => Trace.WriteLine($"\r\n线程{Thread.CurrentThread.ManagedThreadId}: {e.Sql}\r\n");
|
_ormPriv.Aop.CurdBefore += (s, e) => Trace.WriteLine($"\r\n线程{Thread.CurrentThread.ManagedThreadId}: {e.Sql}\r\n");
|
||||||
@ -51,6 +52,7 @@ namespace FreeSql
|
|||||||
_ormPriv.CodeFirst.ConfigEntity(cei.EntityType, cei.Fluent);
|
_ormPriv.CodeFirst.ConfigEntity(cei.EntityType, cei.Fluent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_resolveUow = resolveUow;
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConfigEntityInfo
|
class ConfigEntityInfo
|
||||||
@ -75,72 +77,25 @@ namespace FreeSql
|
|||||||
/// 创建时间
|
/// 创建时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Column(Position = -4)]
|
[Column(Position = -4)]
|
||||||
public 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 DateTime UpdateTime { get; set; }
|
public virtual DateTime UpdateTime { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 逻辑删除
|
/// 逻辑删除
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Column(Position = -2)]
|
[Column(Position = -2)]
|
||||||
public bool IsDeleted { get; set; }
|
public virtual bool IsDeleted { get; set; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 排序
|
/// 排序
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Column(Position = -1)]
|
[Column(Position = -1)]
|
||||||
public int Sort { get; set; }
|
public virtual int Sort { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 开启工作单元事务
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IUnitOfWork Begin() => Begin(null);
|
|
||||||
/// <summary>
|
|
||||||
/// 开启工作单元事务
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="level">事务等级</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IUnitOfWork Begin(IsolationLevel? level)
|
|
||||||
{
|
|
||||||
var uow = Orm.CreateUnitOfWork();
|
|
||||||
uow.IsolationLevel = level;
|
|
||||||
CurrentUnitOfWork = uow;
|
|
||||||
return uow;
|
|
||||||
}
|
|
||||||
|
|
||||||
static readonly AsyncLocal<IUnitOfWork> _AsyncUnitOfWork = new AsyncLocal<IUnitOfWork>();
|
|
||||||
static readonly AsyncLocal<string> _AsyncTenantId = new AsyncLocal<string>();
|
|
||||||
/// <summary>
|
|
||||||
/// 获取或设置当前租户id
|
|
||||||
/// </summary>
|
|
||||||
public static string CurrentTenantId
|
|
||||||
{
|
|
||||||
get => _AsyncTenantId.Value;
|
|
||||||
set => _AsyncTenantId.Value = value;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 获取或设置当前租户id
|
|
||||||
/// </summary>
|
|
||||||
public static IUnitOfWork CurrentUnitOfWork
|
|
||||||
{
|
|
||||||
get => _AsyncUnitOfWork.Value;
|
|
||||||
set => _AsyncUnitOfWork.Value = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 租户
|
|
||||||
/// </summary>
|
|
||||||
public interface ITenant
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 租户id
|
|
||||||
/// </summary>
|
|
||||||
string TenantId { get; set; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Table(DisableSyncStructure = true)]
|
||||||
public abstract class BaseEntityReadOnly<TEntity> : BaseEntity where TEntity : class
|
public abstract class BaseEntityReadOnly<TEntity> : BaseEntity where TEntity : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -153,9 +108,7 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
var select = Orm.Select<TEntity>()
|
var select = Orm.Select<TEntity>()
|
||||||
.TrackToList(TrackToList) //自动为每个元素 Attach
|
.TrackToList(TrackToList) //自动为每个元素 Attach
|
||||||
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction(false));
|
.WithTransaction(_resolveUow?.Invoke()?.GetOrBeginTransaction(false));
|
||||||
if (string.IsNullOrEmpty(CurrentTenantId) == false)
|
|
||||||
select.WhereCascade(a => (a as ITenant).TenantId == CurrentTenantId);
|
|
||||||
return select.WhereCascade(a => (a as BaseEntity).IsDeleted == false);
|
return select.WhereCascade(a => (a as BaseEntity).IsDeleted == false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,6 +122,7 @@ namespace FreeSql
|
|||||||
var ie = list as IEnumerable;
|
var ie = list as IEnumerable;
|
||||||
if (ie == null) return;
|
if (ie == null) return;
|
||||||
var isFirst = true;
|
var isFirst = true;
|
||||||
|
IBaseRepository<TEntity> berepo = null;
|
||||||
foreach (var item in ie)
|
foreach (var item in ie)
|
||||||
{
|
{
|
||||||
if (item == null) return;
|
if (item == null) return;
|
||||||
@ -181,27 +135,29 @@ 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;
|
||||||
}
|
}
|
||||||
(item as BaseEntity<TEntity>)?.Attach();
|
var beitem = item as BaseEntity<TEntity>;
|
||||||
|
if (beitem != null)
|
||||||
|
{
|
||||||
|
if (berepo == null) berepo = Orm.GetRepository<TEntity>();
|
||||||
|
beitem.Repository = berepo;
|
||||||
|
beitem.Attach();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (ls.Any() == false) return;
|
if (ls.Any() == false) return;
|
||||||
if (ls.FirstOrDefault() is BaseEntity<TEntity> == false) return;
|
if (ls.FirstOrDefault() is BaseEntity<TEntity> == false) return;
|
||||||
if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) return;
|
if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) return;
|
||||||
|
IBaseRepository<TEntity> repo = null;
|
||||||
foreach (var item in ls)
|
foreach (var item in ls)
|
||||||
(item as BaseEntity<TEntity>)?.Attach();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 设置当前租户id
|
|
||||||
/// </summary>
|
|
||||||
protected void SetTenantId()
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(CurrentTenantId) == false)
|
|
||||||
{
|
{
|
||||||
var ten = this as ITenant;
|
var beitem = item as BaseEntity<TEntity>;
|
||||||
if (ten != null)
|
if (beitem != null)
|
||||||
ten.TenantId = CurrentTenantId;
|
{
|
||||||
|
if (repo == null) repo = Orm.GetRepository<TEntity>();
|
||||||
|
beitem.Repository = repo;
|
||||||
|
beitem.Attach();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,11 +189,8 @@ namespace FreeSql
|
|||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
var item = this as TEntity;
|
var item = this as TEntity;
|
||||||
this.SetTenantId();
|
|
||||||
this.Repository.Attach(item);
|
this.Repository.Attach(item);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
@ -1,168 +0,0 @@
|
|||||||
#if netcore
|
|
||||||
|
|
||||||
using FreeSql;
|
|
||||||
using FreeSql.DataAnnotations;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 树状基类
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <typeparam name="TKey"></typeparam>
|
|
||||||
[Table(DisableSyncStructure = true)]
|
|
||||||
public abstract class BaseEntityTree<TEntity, TKey> : BaseEntity<TEntity, TKey> where TEntity : class
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 父级id
|
|
||||||
/// </summary>
|
|
||||||
public TKey ParentId
|
|
||||||
{
|
|
||||||
get => _ParentId;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (Equals(value, default(TKey)) == false && Equals(value, Id))
|
|
||||||
throw new ArgumentException("ParentId 值不能与 Id 相同");
|
|
||||||
_ParentId = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public TEntity Parent { get; set; }
|
|
||||||
private TKey _ParentId;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 下级列表
|
|
||||||
/// </summary>
|
|
||||||
[Navigate("ParentId")]
|
|
||||||
public List<TEntity> Childs { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 名称
|
|
||||||
/// </summary>
|
|
||||||
public string Name { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// 名称:技术部-前端
|
|
||||||
/// </summary>
|
|
||||||
public string FullName { get; set; }
|
|
||||||
|
|
||||||
public List<TEntity> GetAllChilds() => Select.WhereDynamic(this)
|
|
||||||
.IncludeMany(a => (a as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t1 => t1.IncludeMany(a1 => (a1 as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t2 => t2.IncludeMany(a2 => (a2 as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t3 => t3.IncludeMany(a3 => (a3 as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t4 => t4.IncludeMany(a4 => (a4 as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t5 => t5.IncludeMany(a5 => (a5 as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t6 => t6.IncludeMany(a6 => (a6 as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t7 => t7.IncludeMany(a7 => (a7 as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t8 => t8.IncludeMany(a8 => (a8 as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t9 => t9.IncludeMany(a9 => (a9 as BaseEntityTree<TEntity, TKey>).Childs,
|
|
||||||
t10 => t10.IncludeMany(a10 => (a10 as BaseEntityTree<TEntity, TKey>).Childs))))))))))).ToList()
|
|
||||||
.SelectMany(a => (a as BaseEntityTree<TEntity, TKey>).Childs
|
|
||||||
.SelectMany(a1 => (a1 as BaseEntityTree<TEntity, TKey>)?.Childs
|
|
||||||
.SelectMany(a2 => (a2 as BaseEntityTree<TEntity, TKey>)?.Childs
|
|
||||||
.SelectMany(a3 => (a3 as BaseEntityTree<TEntity, TKey>)?.Childs
|
|
||||||
.SelectMany(a4 => (a4 as BaseEntityTree<TEntity, TKey>)?.Childs
|
|
||||||
.SelectMany(a5 => (a5 as BaseEntityTree<TEntity, TKey>)?.Childs
|
|
||||||
.SelectMany(a6 => (a6 as BaseEntityTree<TEntity, TKey>)?.Childs
|
|
||||||
.SelectMany(a7 => (a7 as BaseEntityTree<TEntity, TKey>)?.Childs
|
|
||||||
.SelectMany(a8 => (a8 as BaseEntityTree<TEntity, TKey>)?.Childs
|
|
||||||
.SelectMany(a9 => (a9 as BaseEntityTree<TEntity, TKey>)?.Childs
|
|
||||||
.SelectMany(a10 => (a10 as BaseEntityTree<TEntity, TKey>)?.Childs))))))))))).Where(a => a != null).ToList();
|
|
||||||
|
|
||||||
protected void RefershFullName()
|
|
||||||
{
|
|
||||||
var buf = new List<TEntity>();
|
|
||||||
buf.Add(this as TEntity);
|
|
||||||
buf.AddRange(this.GetAllChilds());
|
|
||||||
var repo = Orm.GetRepository<TEntity>();
|
|
||||||
repo.UnitOfWork = CurrentUnitOfWork;
|
|
||||||
buf = repo.Select.WhereDynamic(buf)
|
|
||||||
.Include(a => ((((((((((a as BaseEntityTree<TEntity, TKey>).Parent
|
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent
|
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent
|
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent
|
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent
|
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent
|
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent
|
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent
|
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent
|
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent).ToList(true);
|
|
||||||
foreach (var item in buf)
|
|
||||||
{
|
|
||||||
var up = item as BaseEntityTree<TEntity, TKey>;
|
|
||||||
up.Name = up.Name;
|
|
||||||
var cur = up.Parent as BaseEntityTree<TEntity, TKey>;
|
|
||||||
while (cur != null)
|
|
||||||
{
|
|
||||||
up.Name = $"{cur.Name}-{up.Name}";
|
|
||||||
cur = cur.Parent as BaseEntityTree<TEntity, TKey>;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
repo.Update(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
T UpdateIsDelete<T>(bool value, Func<IBaseRepository<TEntity>, List<TEntity>, T> func)
|
|
||||||
{
|
|
||||||
var childs = GetAllChilds();
|
|
||||||
childs.Add(this as TEntity);
|
|
||||||
var repo = Orm.GetRepository<TEntity>();
|
|
||||||
repo.UnitOfWork = CurrentUnitOfWork;
|
|
||||||
repo.Attach(childs);
|
|
||||||
foreach (var item in childs)
|
|
||||||
(item as BaseEntity).IsDeleted = false;
|
|
||||||
return func(repo, childs);
|
|
||||||
}
|
|
||||||
public override bool Delete(bool physicalDelete = false) => UpdateIsDelete(true, (repo, chis) => repo.Update(chis)) > 0;
|
|
||||||
async public override Task<bool> DeleteAsync(bool physicalDelete = false) => await UpdateIsDelete(true, (repo, chis) => repo.UpdateAsync(chis)) > 0;
|
|
||||||
|
|
||||||
public override bool Restore() => UpdateIsDelete(false, (repo, chis) => repo.Update(chis)) > 0;
|
|
||||||
async public override Task<bool> RestoreAsync() => await UpdateIsDelete(false, (repo, chis) => repo.UpdateAsync(chis)) > 0;
|
|
||||||
|
|
||||||
public override TEntity Insert()
|
|
||||||
{
|
|
||||||
var ret = base.Insert();
|
|
||||||
RefershFullName();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
async public override Task<TEntity> InsertAsync()
|
|
||||||
{
|
|
||||||
var ret = await base.InsertAsync();
|
|
||||||
RefershFullName();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
public override bool Update()
|
|
||||||
{
|
|
||||||
var old = Find(this.Id) as BaseEntityTree<TEntity, TKey>;
|
|
||||||
var ret = base.Update();
|
|
||||||
if (old.Name != this.Name || Equals(old.ParentId, old.ParentId) == false) RefershFullName();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
async public override Task<bool> UpdateAsync()
|
|
||||||
{
|
|
||||||
var old = Find(this.Id) as BaseEntityTree<TEntity, TKey>;
|
|
||||||
var ret = await base.UpdateAsync();
|
|
||||||
if (old.Name != this.Name || Equals(old.ParentId, old.ParentId) == false) RefershFullName();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
public override TEntity Save()
|
|
||||||
{
|
|
||||||
var old = Find(this.Id) as BaseEntityTree<TEntity, TKey>;
|
|
||||||
var ret = base.Save();
|
|
||||||
if (old.Name != this.Name || Equals(old.ParentId, old.ParentId) == false) RefershFullName();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
async public override Task<TEntity> SaveAsync()
|
|
||||||
{
|
|
||||||
var old = Find(this.Id) as BaseEntityTree<TEntity, TKey>;
|
|
||||||
var ret = await base.SaveAsync();
|
|
||||||
if (old.Name != this.Name || Equals(old.ParentId, old.ParentId) == false) RefershFullName();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netcoreapp31;netcoreapp21;net4.0;</TargetFrameworks>
|
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||||
<Version>1.5.0-preview0509</Version>
|
<Version>1.5.0-preview0509</Version>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Authors>ncc;YeXiangQin</Authors>
|
<Authors>ncc;YeXiangQin</Authors>
|
||||||
@ -34,12 +34,8 @@
|
|||||||
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<PropertyGroup Condition="'$(TargetFramework)' == 'net40'">
|
||||||
<Folder Include="Net40\" />
|
<DefineConstants>net40</DefineConstants>
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp31' OR '$(TargetFramework)' == 'netcoreapp21'">
|
|
||||||
<DefineConstants>netcore</DefineConstants>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -68,6 +68,12 @@
|
|||||||
</summary>
|
</summary>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:FreeSql.BaseEntity`1.SaveMany(System.String)">
|
||||||
|
<summary>
|
||||||
|
【完整】保存导航属性,子表
|
||||||
|
</summary>
|
||||||
|
<param name="navigatePropertyName">导航属性名</param>
|
||||||
|
</member>
|
||||||
<member name="T:FreeSql.BaseEntityAsync`2">
|
<member name="T:FreeSql.BaseEntityAsync`2">
|
||||||
<summary>
|
<summary>
|
||||||
包括 CreateTime/UpdateTime/IsDeleted、CRUD 异步方法、以及 ID 主键定义 的实体基类
|
包括 CreateTime/UpdateTime/IsDeleted、CRUD 异步方法、以及 ID 主键定义 的实体基类
|
||||||
@ -125,6 +131,12 @@
|
|||||||
</summary>
|
</summary>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:FreeSql.BaseEntityAsync`1.SaveManyAsync(System.String)">
|
||||||
|
<summary>
|
||||||
|
【完整】保存导航属性,子表
|
||||||
|
</summary>
|
||||||
|
<param name="navigatePropertyName">导航属性名</param>
|
||||||
|
</member>
|
||||||
<member name="T:FreeSql.BaseEntity">
|
<member name="T:FreeSql.BaseEntity">
|
||||||
<summary>
|
<summary>
|
||||||
包括 CreateTime/UpdateTime/IsDeleted 的实体基类
|
包括 CreateTime/UpdateTime/IsDeleted 的实体基类
|
||||||
@ -135,7 +147,7 @@
|
|||||||
全局 IFreeSql orm 对象
|
全局 IFreeSql orm 对象
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:FreeSql.BaseEntity.Initialization(IFreeSql)">
|
<member name="M:FreeSql.BaseEntity.Initialization(IFreeSql,System.Func{FreeSql.IUnitOfWork})">
|
||||||
<summary>
|
<summary>
|
||||||
初始化BaseEntity
|
初始化BaseEntity
|
||||||
BaseEntity.Initialization(new FreeSqlBuilder()
|
BaseEntity.Initialization(new FreeSqlBuilder()
|
||||||
@ -147,6 +159,7 @@
|
|||||||
.Build());
|
.Build());
|
||||||
</summary>
|
</summary>
|
||||||
<param name="fsql">IFreeSql orm 对象</param>
|
<param name="fsql">IFreeSql orm 对象</param>
|
||||||
|
<param name="resolveUow">工作单元(事务)委托,如果不使用事务请传 null<para></para>解释:由于AsyncLocal平台兼容不好,所以交给外部管理</param>
|
||||||
</member>
|
</member>
|
||||||
<member name="P:FreeSql.BaseEntity.CreateTime">
|
<member name="P:FreeSql.BaseEntity.CreateTime">
|
||||||
<summary>
|
<summary>
|
||||||
@ -168,45 +181,12 @@
|
|||||||
排序
|
排序
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:FreeSql.BaseEntity.Begin">
|
|
||||||
<summary>
|
|
||||||
开启工作单元事务
|
|
||||||
</summary>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:FreeSql.BaseEntity.Begin(System.Nullable{System.Data.IsolationLevel})">
|
|
||||||
<summary>
|
|
||||||
开启工作单元事务
|
|
||||||
</summary>
|
|
||||||
<param name="level">事务等级</param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="P:FreeSql.BaseEntity.CurrentTenantId">
|
|
||||||
<summary>
|
|
||||||
获取或设置当前租户id
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="T:FreeSql.ITenant">
|
|
||||||
<summary>
|
|
||||||
租户
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="P:FreeSql.ITenant.TenantId">
|
|
||||||
<summary>
|
|
||||||
租户id
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="P:FreeSql.BaseEntityReadOnly`1.Select">
|
<member name="P:FreeSql.BaseEntityReadOnly`1.Select">
|
||||||
<summary>
|
<summary>
|
||||||
查询数据
|
查询数据
|
||||||
</summary>
|
</summary>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:FreeSql.BaseEntityReadOnly`1.SetTenantId">
|
|
||||||
<summary>
|
|
||||||
设置当前租户id
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="M:FreeSql.BaseEntityReadOnly`1.Where(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
|
<member name="M:FreeSql.BaseEntityReadOnly`1.Where(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
|
||||||
<summary>
|
<summary>
|
||||||
查询条件,Where(a => a.Id > 10),支持导航对象查询,Where(a => a.Author.Email == "2881099@qq.com")
|
查询条件,Where(a => a.Id > 10),支持导航对象查询,Where(a => a.Author.Email == "2881099@qq.com")
|
||||||
@ -232,32 +212,5 @@
|
|||||||
附加实体,在更新数据时,只更新变化的部分
|
附加实体,在更新数据时,只更新变化的部分
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="T:FreeSql.BaseEntityTree`2">
|
|
||||||
<summary>
|
|
||||||
树状基类
|
|
||||||
</summary>
|
|
||||||
<typeparam name="TEntity"></typeparam>
|
|
||||||
<typeparam name="TKey"></typeparam>
|
|
||||||
</member>
|
|
||||||
<member name="P:FreeSql.BaseEntityTree`2.ParentId">
|
|
||||||
<summary>
|
|
||||||
父级id
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="P:FreeSql.BaseEntityTree`2.Childs">
|
|
||||||
<summary>
|
|
||||||
下级列表
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="P:FreeSql.BaseEntityTree`2.Name">
|
|
||||||
<summary>
|
|
||||||
名称
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
<member name="P:FreeSql.BaseEntityTree`2.FullName">
|
|
||||||
<summary>
|
|
||||||
名称:技术部-前端
|
|
||||||
</summary>
|
|
||||||
</member>
|
|
||||||
</members>
|
</members>
|
||||||
</doc>
|
</doc>
|
||||||
|
@ -1,122 +0,0 @@
|
|||||||
#if netcore
|
|
||||||
#else
|
|
||||||
|
|
||||||
using FreeSql;
|
|
||||||
using FreeSql.DataAnnotations;
|
|
||||||
using System;
|
|
||||||
using System.Data;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 包括 CreateTime/UpdateTime/IsDeleted、CRUD 方法、以及 ID 主键定义 的实体基类
|
|
||||||
/// <para></para>
|
|
||||||
/// 当 TKey 为 int/long 时,Id 主键被设为自增值主键
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <typeparam name="TKey"></typeparam>
|
|
||||||
[Table(DisableSyncStructure = true)]
|
|
||||||
public abstract class BaseEntity<TEntity, TKey> : BaseEntity<TEntity> where TEntity : class
|
|
||||||
{
|
|
||||||
static BaseEntity()
|
|
||||||
{
|
|
||||||
var tkeyType = typeof(TKey)?.NullableTypeOrThis();
|
|
||||||
if (tkeyType == typeof(int) || tkeyType == typeof(long))
|
|
||||||
Orm.CodeFirst.ConfigEntity(typeof(TEntity),
|
|
||||||
t => t.Property("Id").IsIdentity(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 主键
|
|
||||||
/// </summary>
|
|
||||||
[Column(Position = 1)]
|
|
||||||
public virtual TKey Id { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 根据主键值获取数据
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="id"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static TEntity Find(TKey id)
|
|
||||||
{
|
|
||||||
var item = Select.WhereDynamic(id).First();
|
|
||||||
(item as BaseEntity<TEntity>)?.Attach();
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 包括 CreateTime/UpdateTime/IsDeleted、以及 CRUD 异步和同步方法的实体基类
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
[Table(DisableSyncStructure = true)]
|
|
||||||
public abstract class BaseEntity<TEntity> : BaseEntityReadOnly<TEntity> where TEntity : class
|
|
||||||
{
|
|
||||||
bool DeletedPrivate(bool value)
|
|
||||||
{
|
|
||||||
if (this.Repository == null)
|
|
||||||
return Orm.Delete<TEntity>(this as TEntity)
|
|
||||||
.ExecuteAffrows() == 1;
|
|
||||||
|
|
||||||
return this.Repository.Delete(this as TEntity) == 1;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 删除数据
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public virtual bool Delete() => this.DeletedPrivate(true);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 更新数据
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public virtual bool Update()
|
|
||||||
{
|
|
||||||
if (this.Repository == null)
|
|
||||||
return Orm.Update<TEntity>()
|
|
||||||
.SetSource(this as TEntity).ExecuteAffrows() == 1;
|
|
||||||
|
|
||||||
return this.Repository.Update(this as TEntity) == 1;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// 插入数据
|
|
||||||
/// </summary>
|
|
||||||
public virtual TEntity Insert()
|
|
||||||
{
|
|
||||||
if (this.Repository == null)
|
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
|
||||||
|
|
||||||
return this.Repository.Insert(this as TEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 更新或插入
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public virtual TEntity Save()
|
|
||||||
{
|
|
||||||
if (this.Repository == null)
|
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
|
||||||
|
|
||||||
return this.Repository.InsertOrUpdate(this as TEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 【完整】保存导航属性,子表
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="navigatePropertyName">导航属性名</param>
|
|
||||||
public virtual void SaveMany(string navigatePropertyName)
|
|
||||||
{
|
|
||||||
if (this.Repository == null)
|
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
|
||||||
|
|
||||||
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,130 +0,0 @@
|
|||||||
#if netcore
|
|
||||||
#else
|
|
||||||
|
|
||||||
using FreeSql.DataAnnotations;
|
|
||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace FreeSql
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 包括 CreateTime/UpdateTime/IsDeleted 的实体基类
|
|
||||||
/// </summary>
|
|
||||||
[Table(DisableSyncStructure = true)]
|
|
||||||
public abstract class BaseEntity
|
|
||||||
{
|
|
||||||
static IFreeSql _ormPriv;
|
|
||||||
/// <summary>
|
|
||||||
/// 全局 IFreeSql orm 对象
|
|
||||||
/// </summary>
|
|
||||||
public static IFreeSql Orm => _ormPriv ?? throw new Exception(@"使用前请初始化 BaseEntity.Initialization(new FreeSqlBuilder()
|
|
||||||
.UseAutoSyncStructure(true)
|
|
||||||
.UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"")
|
|
||||||
.Build());");
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 初始化BaseEntity
|
|
||||||
/// BaseEntity.Initialization(new FreeSqlBuilder()
|
|
||||||
/// <para></para>
|
|
||||||
/// .UseAutoSyncStructure(true)
|
|
||||||
/// <para></para>
|
|
||||||
/// .UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5")
|
|
||||||
/// <para></para>
|
|
||||||
/// .Build());
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="fsql">IFreeSql orm 对象</param>
|
|
||||||
public static void Initialization(IFreeSql fsql)
|
|
||||||
{
|
|
||||||
_ormPriv = fsql;
|
|
||||||
_ormPriv.Aop.CurdBefore += (s, e) => Trace.WriteLine(e.Sql + "\r\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract class BaseEntityReadOnly<TEntity> : BaseEntity where TEntity : class
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 查询数据
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static ISelect<TEntity> Select
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var select = Orm.Select<TEntity>().TrackToList(TrackToList); //自动为每个元素 Attach;
|
|
||||||
return select;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void TrackToList(object list)
|
|
||||||
{
|
|
||||||
if (list == null) return;
|
|
||||||
var ls = list as IList<TEntity>;
|
|
||||||
if (ls == null)
|
|
||||||
{
|
|
||||||
var ie = list as IEnumerable;
|
|
||||||
if (ie == null) return;
|
|
||||||
var isFirst = true;
|
|
||||||
foreach (var item in ie)
|
|
||||||
{
|
|
||||||
if (item == null) return;
|
|
||||||
if (isFirst)
|
|
||||||
{
|
|
||||||
isFirst = false;
|
|
||||||
var itemType = item.GetType();
|
|
||||||
if (itemType == typeof(object)) return;
|
|
||||||
if (itemType.FullName.Contains("FreeSqlLazyEntity__")) itemType = itemType.BaseType;
|
|
||||||
if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return;
|
|
||||||
if (item is BaseEntity<TEntity> == false) return;
|
|
||||||
}
|
|
||||||
(item as BaseEntity<TEntity>)?.Attach();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ls.Any() == false) return;
|
|
||||||
if (ls.FirstOrDefault() is BaseEntity<TEntity> == false) return;
|
|
||||||
if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) return;
|
|
||||||
foreach (var item in ls)
|
|
||||||
(item as BaseEntity<TEntity>)?.Attach();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 查询条件,Where(a => a.Id > 10),支持导航对象查询,Where(a => a.Author.Email == "2881099@qq.com")
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="exp">lambda表达式</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
|
|
||||||
/// <summary>
|
|
||||||
/// 查询条件,Where(true, a => a.Id > 10),支导航对象查询,Where(true, a => a.Author.Email == "2881099@qq.com")
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="condition">true 时生效</param>
|
|
||||||
/// <param name="exp">lambda表达式</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 仓储对象
|
|
||||||
/// </summary>
|
|
||||||
protected IBaseRepository<TEntity> Repository { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 附加实体,在更新数据时,只更新变化的部分
|
|
||||||
/// </summary>
|
|
||||||
public TEntity Attach()
|
|
||||||
{
|
|
||||||
if (this.Repository == null)
|
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
|
||||||
|
|
||||||
var item = this as TEntity;
|
|
||||||
this.Repository.Attach(item);
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -42,7 +42,7 @@ public class UserGroup : BaseEntity<UserGroup, int>
|
|||||||
```csharp
|
```csharp
|
||||||
public class UserGroup : BaseEntity<UserGroup, int>
|
public class UserGroup : BaseEntity<UserGroup, int>
|
||||||
{
|
{
|
||||||
[Column(IsIdentity = false)]
|
[Column(IsIdentity = false)]
|
||||||
public override int Id { get; set; }
|
public override int Id { get; set; }
|
||||||
public string GroupName { get; set; }
|
public string GroupName { get; set; }
|
||||||
}
|
}
|
||||||
@ -90,3 +90,37 @@ var items = UserGroup.Where(a => a.Id > 10).ToList();
|
|||||||
支持多表查询时,软删除条件会附加在每个表中;
|
支持多表查询时,软删除条件会附加在每个表中;
|
||||||
|
|
||||||
> 有关更多查询方法,请参考资料:https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2
|
> 有关更多查询方法,请参考资料:https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2
|
||||||
|
|
||||||
|
# 事务建议
|
||||||
|
|
||||||
|
由于 AsyncLocal 平台兼容不好,所以交给外部管理。
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
static AsyncLocal<IUnitOfWork> _asyncUow = new AsyncLocal<IUnitOfWork>();
|
||||||
|
|
||||||
|
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
|
||||||
|
```
|
||||||
|
|
||||||
|
在 Scoped 开始时: _asyncUow.Value = fsql.CreateUnitOfWork(); (也可以使用 UnitOfWorkManager 对象获取 uow)
|
||||||
|
|
||||||
|
在 Scoped 结束时:_asyncUow.Value = null;
|
||||||
|
|
||||||
|
如下:
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
using (var uow = fsql.CreateUnitOfWork())
|
||||||
|
{
|
||||||
|
_asyncUow.Value = uow;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//todo ...
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_asyncUow.Value = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
uow.Commit();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@ -39,7 +39,7 @@ Namespace FreeSql.Tests.VB
|
|||||||
|
|
||||||
Dim List9 = g.sqlserver.Select(Of Testvb).IncludeMany(Function(a) a.Testvb2s).ToList()
|
Dim List9 = g.sqlserver.Select(Of Testvb).IncludeMany(Function(a) a.Testvb2s).ToList()
|
||||||
|
|
||||||
BaseEntity.Initialization(g.sqlserver)
|
BaseEntity.Initialization(g.sqlserver, Nothing)
|
||||||
Dim cowR As CowRecord = New CowRecord
|
Dim cowR As CowRecord = New CowRecord
|
||||||
cowR.Id = 1
|
cowR.Id = 1
|
||||||
cowR.Lact = 1
|
cowR.Lact = 1
|
||||||
|
@ -24,7 +24,7 @@ namespace FreeSql.Tests
|
|||||||
g.sqlite.Delete<userinfo>().Where("1=1").ExecuteAffrows();
|
g.sqlite.Delete<userinfo>().Where("1=1").ExecuteAffrows();
|
||||||
g.sqlite.Delete<DEPARTMENTS>().Where("1=1").ExecuteAffrows();
|
g.sqlite.Delete<DEPARTMENTS>().Where("1=1").ExecuteAffrows();
|
||||||
g.sqlite.Delete<dept_user>().Where("1=1").ExecuteAffrows();
|
g.sqlite.Delete<dept_user>().Where("1=1").ExecuteAffrows();
|
||||||
BaseEntity.Initialization(g.sqlite);
|
BaseEntity.Initialization(g.sqlite, null);
|
||||||
|
|
||||||
userinfo user = new userinfo { userid = 1, badgenumber = "", Name="", IDCardNo="" };
|
userinfo user = new userinfo { userid = 1, badgenumber = "", Name="", IDCardNo="" };
|
||||||
user.Insert();
|
user.Insert();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user