mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
移除 FreeSql.DbContext,成为独立项目
This commit is contained in:
parent
3ca91b70a9
commit
be5259dd68
@ -41,12 +41,6 @@ namespace orm_vs
|
|||||||
//optionsBuilder.UseMySql("Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=21;Max Pool Size=21");
|
//optionsBuilder.UseMySql("Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=21;Max Pool Size=21");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class FreeSongContext: FreeSql.DbContext {
|
|
||||||
public FreeSql.DbSet<Song> Songs { get; set; }
|
|
||||||
protected override void OnConfiguring(FreeSql.DbContextOptionsBuilder builder) {
|
|
||||||
builder.UseFreeSql(fsql);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Main(string[] args) {
|
static void Main(string[] args) {
|
||||||
|
|
||||||
|
@ -12,7 +12,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
|
||||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -1,87 +0,0 @@
|
|||||||
using SafeObjectPool;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public abstract partial class DbContext : IDisposable {
|
|
||||||
|
|
||||||
internal IFreeSql _orm;
|
|
||||||
internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
|
|
||||||
|
|
||||||
IUnitOfWork _uowPriv;
|
|
||||||
internal IUnitOfWork _uow => _isUseUnitOfWork ? (_uowPriv ?? (_uowPriv = new UnitOfWork(_fsql))) : null;
|
|
||||||
internal bool _isUseUnitOfWork = true; //不使用工作单元事务
|
|
||||||
|
|
||||||
static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
|
|
||||||
protected DbContext() {
|
|
||||||
|
|
||||||
var builder = new DbContextOptionsBuilder();
|
|
||||||
OnConfiguring(builder);
|
|
||||||
_orm = builder._fsql;
|
|
||||||
|
|
||||||
var props = _dicGetDbSetProps.GetOrAdd(this.GetType(), tp =>
|
|
||||||
tp.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
|
|
||||||
.Where(a => a.PropertyType.IsGenericType &&
|
|
||||||
a.PropertyType == typeof(DbSet<>).MakeGenericType(a.PropertyType.GenericTypeArguments[0])).ToArray());
|
|
||||||
|
|
||||||
foreach (var prop in props) {
|
|
||||||
var set = this.Set(prop.PropertyType.GenericTypeArguments[0]);
|
|
||||||
|
|
||||||
prop.SetValue(this, set);
|
|
||||||
AllSets.Add(prop.Name, set);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected Dictionary<Type, object> _dicSet = new Dictionary<Type, object>();
|
|
||||||
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
|
|
||||||
public virtual object Set(Type entityType) {
|
|
||||||
if (_dicSet.ContainsKey(entityType)) return _dicSet[entityType];
|
|
||||||
var sd = Activator.CreateInstance(typeof(DbContextDbSet<>).MakeGenericType(entityType), this);
|
|
||||||
_dicSet.Add(entityType, sd);
|
|
||||||
return sd;
|
|
||||||
}
|
|
||||||
protected Dictionary<string, object> AllSets { get; } = new Dictionary<string, object>();
|
|
||||||
|
|
||||||
internal class ExecCommandInfo {
|
|
||||||
public ExecCommandInfoType actionType { get; set; }
|
|
||||||
public object dbSet { get; set; }
|
|
||||||
public Type stateType { get; set; }
|
|
||||||
public object state { get; set; }
|
|
||||||
}
|
|
||||||
internal enum ExecCommandInfoType { Insert, Update, Delete }
|
|
||||||
Queue<ExecCommandInfo> _actions = new Queue<ExecCommandInfo>();
|
|
||||||
internal int _affrows = 0;
|
|
||||||
|
|
||||||
internal void EnqueueAction(ExecCommandInfoType actionType, object dbSet, Type stateType, object state) {
|
|
||||||
_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
|
|
||||||
}
|
|
||||||
|
|
||||||
~DbContext() {
|
|
||||||
this.Dispose();
|
|
||||||
}
|
|
||||||
bool _isdisposed = false;
|
|
||||||
public void Dispose() {
|
|
||||||
if (_isdisposed) return;
|
|
||||||
try {
|
|
||||||
_actions.Clear();
|
|
||||||
_dicSet.Clear();
|
|
||||||
AllSets.Clear();
|
|
||||||
|
|
||||||
_uow?.Rollback();
|
|
||||||
} finally {
|
|
||||||
_isdisposed = true;
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,118 +0,0 @@
|
|||||||
using SafeObjectPool;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
partial class DbContext {
|
|
||||||
|
|
||||||
async public Task<int> SaveChangesAsync() {
|
|
||||||
await ExecCommandAsync();
|
|
||||||
_uow?.Commit();
|
|
||||||
var ret = _affrows;
|
|
||||||
_affrows = 0;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>> _dicExecCommandDbContextBetchAsync = new Dictionary<Type, Dictionary<string, Func<object, object[], Task<int>>>>();
|
|
||||||
async internal Task ExecCommandAsync() {
|
|
||||||
if (_actions.Any() == false) return;
|
|
||||||
ExecCommandInfo oldinfo = null;
|
|
||||||
var states = new List<object>();
|
|
||||||
|
|
||||||
Func<string, Task<int>> dbContextBetch = methodName => {
|
|
||||||
if (_dicExecCommandDbContextBetchAsync.TryGetValue(oldinfo.stateType, out var trydic) == false)
|
|
||||||
trydic = new Dictionary<string, Func<object, object[], Task<int>>>();
|
|
||||||
if (trydic.TryGetValue(methodName, out var tryfunc) == false) {
|
|
||||||
var arrType = oldinfo.stateType.MakeArrayType();
|
|
||||||
var dbsetType = oldinfo.dbSet.GetType().BaseType;
|
|
||||||
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
|
|
||||||
|
|
||||||
var returnTarget = Expression.Label(typeof(Task<int>));
|
|
||||||
var parm1DbSet = Expression.Parameter(typeof(object));
|
|
||||||
var parm2Vals = Expression.Parameter(typeof(object[]));
|
|
||||||
var var1Vals = Expression.Variable(arrType);
|
|
||||||
tryfunc = Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
|
|
||||||
new[] { var1Vals },
|
|
||||||
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
|
||||||
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
|
|
||||||
Expression.Label(returnTarget, Expression.Default(typeof(int)))
|
|
||||||
), new[] { parm1DbSet, parm2Vals }).Compile();
|
|
||||||
trydic.Add(methodName, tryfunc);
|
|
||||||
}
|
|
||||||
return tryfunc(oldinfo.dbSet, states.ToArray());
|
|
||||||
};
|
|
||||||
Func<Task> funcDelete = async () => {
|
|
||||||
_affrows += await dbContextBetch("DbContextBetchRemoveAsync");
|
|
||||||
states.Clear();
|
|
||||||
};
|
|
||||||
Func<Task> funcInsert = async () => {
|
|
||||||
_affrows += await dbContextBetch("DbContextBetchAddAsync");
|
|
||||||
states.Clear();
|
|
||||||
};
|
|
||||||
Func<bool, Task> funcUpdate = async (isLiveUpdate) => {
|
|
||||||
var affrows = 0;
|
|
||||||
if (isLiveUpdate) affrows = await dbContextBetch("DbContextBetchUpdateNowAsync");
|
|
||||||
else affrows = await dbContextBetch("DbContextBetchUpdateAsync");
|
|
||||||
if (affrows == -999) { //最后一个元素已被删除
|
|
||||||
states.RemoveAt(states.Count - 1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (affrows == -998 || affrows == -997) { //没有执行更新
|
|
||||||
var laststate = states[states.Count - 1];
|
|
||||||
states.Clear();
|
|
||||||
if (affrows == -997) states.Add(laststate); //保留最后一个
|
|
||||||
}
|
|
||||||
if (affrows > 0) {
|
|
||||||
_affrows += affrows;
|
|
||||||
var islastNotUpdated = states.Count != affrows;
|
|
||||||
var laststate = states[states.Count - 1];
|
|
||||||
states.Clear();
|
|
||||||
if (islastNotUpdated) states.Add(laststate); //保留最后一个
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
while (_actions.Any() || states.Any()) {
|
|
||||||
var info = _actions.Any() ? _actions.Dequeue() : null;
|
|
||||||
if (oldinfo == null) oldinfo = info;
|
|
||||||
var isLiveUpdate = false;
|
|
||||||
|
|
||||||
if (_actions.Any() == false && states.Any() ||
|
|
||||||
info != null && oldinfo.actionType != info.actionType ||
|
|
||||||
info != null && oldinfo.stateType != info.stateType) {
|
|
||||||
|
|
||||||
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType) {
|
|
||||||
//最后一个,合起来发送
|
|
||||||
states.Add(info.state);
|
|
||||||
info = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (oldinfo.actionType) {
|
|
||||||
case ExecCommandInfoType.Insert:
|
|
||||||
await funcInsert();
|
|
||||||
break;
|
|
||||||
case ExecCommandInfoType.Delete:
|
|
||||||
await funcDelete();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
isLiveUpdate = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isLiveUpdate || oldinfo.actionType == ExecCommandInfoType.Update) {
|
|
||||||
if (states.Any())
|
|
||||||
await funcUpdate(isLiveUpdate);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info != null) {
|
|
||||||
states.Add(info.state);
|
|
||||||
oldinfo = info;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
using Microsoft.Extensions.Caching.Distributed;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public class DbContextOptionsBuilder {
|
|
||||||
|
|
||||||
internal IFreeSql _fsql;
|
|
||||||
|
|
||||||
public DbContextOptionsBuilder UseFreeSql(IFreeSql orm) {
|
|
||||||
_fsql = orm;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,117 +0,0 @@
|
|||||||
using SafeObjectPool;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
partial class DbContext {
|
|
||||||
|
|
||||||
public int SaveChanges() {
|
|
||||||
ExecCommand();
|
|
||||||
_uow?.Commit();
|
|
||||||
var ret = _affrows;
|
|
||||||
_affrows = 0;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Dictionary<Type, Dictionary<string, Func<object, object[], int>>> _dicExecCommandDbContextBetch = new Dictionary<Type, Dictionary<string, Func<object, object[], int>>>();
|
|
||||||
internal void ExecCommand() {
|
|
||||||
if (_actions.Any() == false) return;
|
|
||||||
ExecCommandInfo oldinfo = null;
|
|
||||||
var states = new List<object>();
|
|
||||||
|
|
||||||
Func<string, int> dbContextBetch = methodName => {
|
|
||||||
if (_dicExecCommandDbContextBetch.TryGetValue(oldinfo.stateType, out var trydic) == false)
|
|
||||||
trydic = new Dictionary<string, Func<object, object[], int>>();
|
|
||||||
if (trydic.TryGetValue(methodName, out var tryfunc) == false) {
|
|
||||||
var arrType = oldinfo.stateType.MakeArrayType();
|
|
||||||
var dbsetType = oldinfo.dbSet.GetType().BaseType;
|
|
||||||
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
|
|
||||||
|
|
||||||
var returnTarget = Expression.Label(typeof(int));
|
|
||||||
var parm1DbSet = Expression.Parameter(typeof(object));
|
|
||||||
var parm2Vals = Expression.Parameter(typeof(object[]));
|
|
||||||
var var1Vals = Expression.Variable(arrType);
|
|
||||||
tryfunc = Expression.Lambda<Func<object, object[], int>>(Expression.Block(
|
|
||||||
new[] { var1Vals },
|
|
||||||
Expression.Assign(var1Vals, Expression.Convert(FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
|
|
||||||
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
|
|
||||||
Expression.Label(returnTarget, Expression.Default(typeof(int)))
|
|
||||||
), new[] { parm1DbSet, parm2Vals }).Compile();
|
|
||||||
trydic.Add(methodName, tryfunc);
|
|
||||||
}
|
|
||||||
return tryfunc(oldinfo.dbSet, states.ToArray());
|
|
||||||
};
|
|
||||||
Action funcDelete = () => {
|
|
||||||
_affrows += dbContextBetch("DbContextBetchRemove");
|
|
||||||
states.Clear();
|
|
||||||
};
|
|
||||||
Action funcInsert = () => {
|
|
||||||
_affrows += dbContextBetch("DbContextBetchAdd");
|
|
||||||
states.Clear();
|
|
||||||
};
|
|
||||||
Action<bool> funcUpdate = isLiveUpdate => {
|
|
||||||
var affrows = 0;
|
|
||||||
if (isLiveUpdate) affrows = dbContextBetch("DbContextBetchUpdateNow");
|
|
||||||
else affrows = dbContextBetch("DbContextBetchUpdate");
|
|
||||||
if (affrows == -999) { //最后一个元素已被删除
|
|
||||||
states.RemoveAt(states.Count - 1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (affrows == -998 || affrows == -997) { //没有执行更新
|
|
||||||
var laststate = states[states.Count - 1];
|
|
||||||
states.Clear();
|
|
||||||
if (affrows == -997) states.Add(laststate); //保留最后一个
|
|
||||||
}
|
|
||||||
if (affrows > 0) {
|
|
||||||
_affrows += affrows;
|
|
||||||
var islastNotUpdated = states.Count != affrows;
|
|
||||||
var laststate = states[states.Count - 1];
|
|
||||||
states.Clear();
|
|
||||||
if (islastNotUpdated) states.Add(laststate); //保留最后一个
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
while (_actions.Any() || states.Any()) {
|
|
||||||
var info = _actions.Any() ? _actions.Dequeue() : null;
|
|
||||||
if (oldinfo == null) oldinfo = info;
|
|
||||||
var isLiveUpdate = false;
|
|
||||||
|
|
||||||
if (_actions.Any() == false && states.Any() ||
|
|
||||||
info != null && oldinfo.actionType != info.actionType ||
|
|
||||||
info != null && oldinfo.stateType != info.stateType) {
|
|
||||||
|
|
||||||
if (info != null && oldinfo.actionType == info.actionType && oldinfo.stateType == info.stateType) {
|
|
||||||
//最后一个,合起来发送
|
|
||||||
states.Add(info.state);
|
|
||||||
info = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (oldinfo.actionType) {
|
|
||||||
case ExecCommandInfoType.Insert:
|
|
||||||
funcInsert();
|
|
||||||
break;
|
|
||||||
case ExecCommandInfoType.Delete:
|
|
||||||
funcDelete();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
isLiveUpdate = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isLiveUpdate || oldinfo.actionType == ExecCommandInfoType.Update) {
|
|
||||||
if (states.Any())
|
|
||||||
funcUpdate(isLiveUpdate);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info != null) {
|
|
||||||
states.Add(info.state);
|
|
||||||
oldinfo = info;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,215 +0,0 @@
|
|||||||
using FreeSql.Extensions.EntityUtil;
|
|
||||||
using FreeSql.Internal.Model;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
|
|
||||||
internal class DbContextDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
|
||||||
|
|
||||||
public DbContextDbSet(DbContext ctx) {
|
|
||||||
_ctx = ctx;
|
|
||||||
_uow = ctx._uow;
|
|
||||||
_fsql = ctx._fsql;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract partial class DbSet<TEntity> : IDisposable where TEntity : class {
|
|
||||||
|
|
||||||
internal DbContext _ctx;
|
|
||||||
internal IUnitOfWork _uow;
|
|
||||||
internal IFreeSql _fsql;
|
|
||||||
|
|
||||||
protected virtual ISelect<TEntity> OrmSelect(object dywhere) {
|
|
||||||
DbContextExecCommand(); //查询前先提交,否则会出脏读
|
|
||||||
return _fsql.Select<TEntity>(dywhere).WithTransaction(_uow?.GetOrBeginTransaction(false)).TrackToList(TrackToList);
|
|
||||||
}
|
|
||||||
|
|
||||||
~DbSet() {
|
|
||||||
this.Dispose();
|
|
||||||
}
|
|
||||||
bool _isdisposed = false;
|
|
||||||
public void Dispose() {
|
|
||||||
if (_isdisposed) return;
|
|
||||||
try {
|
|
||||||
this._dicUpdateTimes.Clear();
|
|
||||||
this._states.Clear();
|
|
||||||
} finally {
|
|
||||||
_isdisposed = true;
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual IInsert<TEntity> OrmInsert() => _fsql.Insert<TEntity>().WithTransaction(_uow?.GetOrBeginTransaction());
|
|
||||||
protected virtual IInsert<TEntity> OrmInsert(TEntity data) => _fsql.Insert<TEntity>(data).WithTransaction(_uow?.GetOrBeginTransaction());
|
|
||||||
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data) => _fsql.Insert<TEntity>(data).WithTransaction(_uow?.GetOrBeginTransaction());
|
|
||||||
|
|
||||||
protected virtual IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys) => _fsql.Update<TEntity>().SetSource(entitys).WithTransaction(_uow?.GetOrBeginTransaction());
|
|
||||||
protected virtual IDelete<TEntity> OrmDelete(object dywhere) => _fsql.Delete<TEntity>(dywhere).WithTransaction(_uow?.GetOrBeginTransaction());
|
|
||||||
|
|
||||||
internal void EnqueueToDbContext(DbContext.ExecCommandInfoType actionType, EntityState state) {
|
|
||||||
_ctx.EnqueueAction(actionType, this, typeof(EntityState), state);
|
|
||||||
}
|
|
||||||
internal void IncrAffrows(int affrows) {
|
|
||||||
_ctx._affrows += affrows;
|
|
||||||
}
|
|
||||||
internal void TrackToList(object list) {
|
|
||||||
if (list == null) return;
|
|
||||||
var ls = list as IList<TEntity>;
|
|
||||||
if (ls == null) return;
|
|
||||||
|
|
||||||
foreach (var item in ls) {
|
|
||||||
var key = _fsql.GetEntityKeyString(item);
|
|
||||||
if (_states.ContainsKey(key)) {
|
|
||||||
_fsql.MapEntityValue(item, _states[key].Value);
|
|
||||||
_states[key].Time = DateTime.Now;
|
|
||||||
} else {
|
|
||||||
_states.Add(key, CreateEntityState(item));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ISelect<TEntity> Select => this.OrmSelect(null);
|
|
||||||
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).Where(exp);
|
|
||||||
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).WhereIf(condition, exp);
|
|
||||||
|
|
||||||
protected Dictionary<string, EntityState> _states = new Dictionary<string, EntityState>();
|
|
||||||
internal Dictionary<string, EntityState> _statesInternal => _states;
|
|
||||||
TableInfo _tablePriv;
|
|
||||||
protected TableInfo _table => _tablePriv ?? (_tablePriv = _fsql.CodeFirst.GetTableByEntity(_entityType));
|
|
||||||
ColumnInfo[] _tableIdentitysPriv;
|
|
||||||
protected ColumnInfo[] _tableIdentitys => _tableIdentitysPriv ?? (_tableIdentitysPriv = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray());
|
|
||||||
protected Type _entityType = typeof(TEntity);
|
|
||||||
internal Type _entityTypeInternal => _entityType;
|
|
||||||
|
|
||||||
public class EntityState {
|
|
||||||
public EntityState(TEntity value, string key) {
|
|
||||||
this.Value = value;
|
|
||||||
this.Key = key;
|
|
||||||
this.Time = DateTime.Now;
|
|
||||||
}
|
|
||||||
public TEntity OldValue { get; set; }
|
|
||||||
public TEntity Value { get; set; }
|
|
||||||
public string Key { get; set; }
|
|
||||||
public DateTime Time { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Utils
|
|
||||||
EntityState CreateEntityState(TEntity data) {
|
|
||||||
if (data == null) throw new ArgumentNullException(nameof(data));
|
|
||||||
var key = _fsql.GetEntityKeyString(data);
|
|
||||||
var state = new EntityState(Activator.CreateInstance<TEntity>(), key);
|
|
||||||
_fsql.MapEntityValue(data, state.Value);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
bool ExistsInStates(TEntity data) {
|
|
||||||
if (data == null) throw new ArgumentNullException(nameof(data));
|
|
||||||
var key = _fsql.GetEntityKeyString(data);
|
|
||||||
if (string.IsNullOrEmpty(key)) return false;
|
|
||||||
return _states.ContainsKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CanAdd(IEnumerable<TEntity> data, bool isThrow) {
|
|
||||||
if (data == null) {
|
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
foreach (var s in data) if (CanAdd(s, isThrow) == false) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
bool CanAdd(TEntity data, bool isThrow) {
|
|
||||||
if (data == null) {
|
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var key = _fsql.GetEntityKeyString(data);
|
|
||||||
if (string.IsNullOrEmpty(key)) {
|
|
||||||
switch (_fsql.Ado.DataType) {
|
|
||||||
case DataType.SqlServer:
|
|
||||||
case DataType.PostgreSQL:
|
|
||||||
return true;
|
|
||||||
case DataType.MySql:
|
|
||||||
case DataType.Oracle:
|
|
||||||
case DataType.Sqlite:
|
|
||||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (isThrow) throw new Exception($"不可添加,未设置主键的值:{_fsql.GetEntityString(data)}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (_states.ContainsKey(key)) {
|
|
||||||
if (isThrow) throw new Exception($"不可添加,已存在于状态管理:{_fsql.GetEntityString(data)}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var idval = _fsql.GetEntityIdentityValueWithPrimary(data);
|
|
||||||
if (idval > 0) {
|
|
||||||
if (isThrow) throw new Exception($"不可添加,自增属性有值:{_fsql.GetEntityString(data)}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CanUpdate(IEnumerable<TEntity> data, bool isThrow) {
|
|
||||||
if (data == null) {
|
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
foreach (var s in data) if (CanUpdate(s, isThrow) == false) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
bool CanUpdate(TEntity data, bool isThrow) {
|
|
||||||
if (data == null) {
|
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (_table.Primarys.Any() == false) {
|
|
||||||
if (isThrow) throw new Exception($"不可更新,实体没有主键:{_fsql.GetEntityString(data)}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var key = _fsql.GetEntityKeyString(data);
|
|
||||||
if (string.IsNullOrEmpty(key)) {
|
|
||||||
if (isThrow) throw new Exception($"不可更新,未设置主键的值:{_fsql.GetEntityString(data)}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (_states.TryGetValue(key, out var tryval) == false) {
|
|
||||||
if (isThrow) throw new Exception($"不可更新,数据未被跟踪,应该先查询:{_fsql.GetEntityString(data)}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CanRemove(IEnumerable<TEntity> data, bool isThrow) {
|
|
||||||
if (data == null) {
|
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
foreach (var s in data) if (CanRemove(s, isThrow) == false) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
bool CanRemove(TEntity data, bool isThrow) {
|
|
||||||
if (data == null) {
|
|
||||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (_table.Primarys.Any() == false) {
|
|
||||||
if (isThrow) throw new Exception($"不可删除,实体没有主键:{_fsql.GetEntityString(data)}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var key = _fsql.GetEntityKeyString(data);
|
|
||||||
if (string.IsNullOrEmpty(key)) {
|
|
||||||
if (isThrow) throw new Exception($"不可删除,未设置主键的值:{_fsql.GetEntityString(data)}");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
//if (_states.TryGetValue(key, out var tryval) == false) {
|
|
||||||
// if (isThrow) throw new Exception($"不可删除,数据未被跟踪,应该先查询:{_fsql.GetEntityString(data)}");
|
|
||||||
// return false;
|
|
||||||
//}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,158 +0,0 @@
|
|||||||
using FreeSql.Extensions.EntityUtil;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
partial class DbSet<TEntity> {
|
|
||||||
|
|
||||||
Task DbContextExecCommandAsync() {
|
|
||||||
_dicUpdateTimes.Clear();
|
|
||||||
return _ctx.ExecCommandAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
async Task<int> DbContextBetchAddAsync(EntityState[] adds) {
|
|
||||||
if (adds.Any() == false) return 0;
|
|
||||||
var affrows = await this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrowsAsync();
|
|
||||||
return affrows;
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Add
|
|
||||||
async Task AddPrivAsync(TEntity data, bool isCheck) {
|
|
||||||
if (isCheck && CanAdd(data, true) == false) return;
|
|
||||||
if (_tableIdentitys.Length > 0) {
|
|
||||||
//有自增,马上执行
|
|
||||||
switch (_fsql.Ado.DataType) {
|
|
||||||
case DataType.SqlServer:
|
|
||||||
case DataType.PostgreSQL:
|
|
||||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
|
||||||
await DbContextExecCommandAsync();
|
|
||||||
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
|
|
||||||
IncrAffrows(1);
|
|
||||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
|
||||||
var state = CreateEntityState(data);
|
|
||||||
_states.Add(state.Key, state);
|
|
||||||
} else {
|
|
||||||
await DbContextExecCommandAsync();
|
|
||||||
var newval = (await this.OrmInsert(data).ExecuteInsertedAsync()).First();
|
|
||||||
IncrAffrows(1);
|
|
||||||
_fsql.MapEntityValue(newval, data);
|
|
||||||
var state = CreateEntityState(newval);
|
|
||||||
_states.Add(state.Key, state);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
case DataType.MySql:
|
|
||||||
case DataType.Oracle:
|
|
||||||
case DataType.Sqlite:
|
|
||||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
|
||||||
await DbContextExecCommandAsync();
|
|
||||||
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
|
|
||||||
IncrAffrows(1);
|
|
||||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
|
||||||
var state = CreateEntityState(data);
|
|
||||||
_states.Add(state.Key, state);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public Task AddAsync(TEntity data) => AddPrivAsync(data, true);
|
|
||||||
async public Task AddRangeAsync(IEnumerable<TEntity> data) {
|
|
||||||
if (CanAdd(data, true) == false) return;
|
|
||||||
if (data.ElementAtOrDefault(1) == default(TEntity)) {
|
|
||||||
await AddAsync(data.First());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (_tableIdentitys.Length > 0) {
|
|
||||||
//有自增,马上执行
|
|
||||||
switch (_fsql.Ado.DataType) {
|
|
||||||
case DataType.SqlServer:
|
|
||||||
case DataType.PostgreSQL:
|
|
||||||
await DbContextExecCommandAsync();
|
|
||||||
var rets = await this.OrmInsert(data).ExecuteInsertedAsync();
|
|
||||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
|
||||||
var idx = 0;
|
|
||||||
foreach (var s in data)
|
|
||||||
_fsql.MapEntityValue(rets[idx++], s);
|
|
||||||
IncrAffrows(rets.Count);
|
|
||||||
TrackToList(rets);
|
|
||||||
return;
|
|
||||||
case DataType.MySql:
|
|
||||||
case DataType.Oracle:
|
|
||||||
case DataType.Sqlite:
|
|
||||||
foreach (var s in data)
|
|
||||||
await AddPrivAsync(s, false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//进入队列,等待 SaveChanges 时执行
|
|
||||||
foreach (var item in data)
|
|
||||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(item));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region UpdateAsync
|
|
||||||
Task<int> DbContextBetchUpdateAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, false);
|
|
||||||
Task<int> DbContextBetchUpdateNowAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, true);
|
|
||||||
async Task<int> DbContextBetchUpdatePrivAsync(EntityState[] ups, bool isLiveUpdate) {
|
|
||||||
if (ups.Any() == false) return 0;
|
|
||||||
var uplst1 = ups[ups.Length - 1];
|
|
||||||
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
|
|
||||||
|
|
||||||
if (_states.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
|
|
||||||
var lstval2 = default(EntityState);
|
|
||||||
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"特别错误:更新失败,数据未被跟踪:{_fsql.GetEntityString(uplst2.Value)}");
|
|
||||||
|
|
||||||
var cuig1 = _fsql.CompareEntityValueReturnColumns(uplst1.Value, lstval1.Value, true);
|
|
||||||
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(uplst2.Value, lstval2.Value, true) : null;
|
|
||||||
|
|
||||||
List<EntityState> data = null;
|
|
||||||
string[] cuig = null;
|
|
||||||
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
|
|
||||||
//最后一个不保存
|
|
||||||
data = ups.ToList();
|
|
||||||
data.RemoveAt(ups.Length - 1);
|
|
||||||
cuig = cuig2;
|
|
||||||
} else if (isLiveUpdate) {
|
|
||||||
//立即保存
|
|
||||||
data = ups.ToList();
|
|
||||||
cuig = cuig1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data?.Count > 0) {
|
|
||||||
|
|
||||||
if (cuig.Length == _table.Columns.Count)
|
|
||||||
return ups.Length == data.Count ? -998 : -997;
|
|
||||||
|
|
||||||
var updateSource = data.Select(a => a.Value).ToArray();
|
|
||||||
var update = this.OrmUpdate(null).SetSource(updateSource).IgnoreColumns(cuig);
|
|
||||||
|
|
||||||
var affrows = await update.ExecuteAffrowsAsync();
|
|
||||||
|
|
||||||
foreach (var newval in data) {
|
|
||||||
if (_states.TryGetValue(newval.Key, out var tryold))
|
|
||||||
_fsql.MapEntityValue(newval.Value, tryold.Value);
|
|
||||||
if (newval.OldValue != null)
|
|
||||||
_fsql.MapEntityValue(newval.Value, newval.OldValue);
|
|
||||||
}
|
|
||||||
return affrows;
|
|
||||||
}
|
|
||||||
|
|
||||||
//等待下次对比再保存
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region RemoveAsync
|
|
||||||
async Task<int> DbContextBetchRemoveAsync(EntityState[] dels) {
|
|
||||||
if (dels.Any() == false) return 0;
|
|
||||||
var affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync();
|
|
||||||
return Math.Max(dels.Length, affrows);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,183 +0,0 @@
|
|||||||
using FreeSql.Extensions.EntityUtil;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
partial class DbSet<TEntity> {
|
|
||||||
|
|
||||||
void DbContextExecCommand() {
|
|
||||||
_dicUpdateTimes.Clear();
|
|
||||||
_ctx.ExecCommand();
|
|
||||||
}
|
|
||||||
|
|
||||||
int DbContextBetchAdd(EntityState[] adds) {
|
|
||||||
if (adds.Any() == false) return 0;
|
|
||||||
var affrows = this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrows();
|
|
||||||
return affrows;
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Add
|
|
||||||
void AddPriv(TEntity data, bool isCheck) {
|
|
||||||
if (isCheck && CanAdd(data, true) == false) return;
|
|
||||||
if (_tableIdentitys.Length > 0) {
|
|
||||||
//有自增,马上执行
|
|
||||||
switch (_fsql.Ado.DataType) {
|
|
||||||
case DataType.SqlServer:
|
|
||||||
case DataType.PostgreSQL:
|
|
||||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
|
||||||
DbContextExecCommand();
|
|
||||||
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
|
||||||
IncrAffrows(1);
|
|
||||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
|
||||||
var state = CreateEntityState(data);
|
|
||||||
_states.Add(state.Key, state);
|
|
||||||
} else {
|
|
||||||
DbContextExecCommand();
|
|
||||||
var newval = this.OrmInsert(data).ExecuteInserted().First();
|
|
||||||
IncrAffrows(1);
|
|
||||||
_fsql.MapEntityValue(newval, data);
|
|
||||||
var state = CreateEntityState(newval);
|
|
||||||
_states.Add(state.Key, state);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
case DataType.MySql:
|
|
||||||
case DataType.Oracle:
|
|
||||||
case DataType.Sqlite:
|
|
||||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
|
||||||
DbContextExecCommand();
|
|
||||||
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
|
||||||
IncrAffrows(1);
|
|
||||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
|
||||||
var state = CreateEntityState(data);
|
|
||||||
_states.Add(state.Key, state);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
|
||||||
}
|
|
||||||
public void Add(TEntity data) => AddPriv(data, true);
|
|
||||||
public void AddRange(IEnumerable<TEntity> data) {
|
|
||||||
if (CanAdd(data, true) == false) return;
|
|
||||||
if (data.ElementAtOrDefault(1) == default(TEntity)) {
|
|
||||||
Add(data.First());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (_tableIdentitys.Length > 0) {
|
|
||||||
//有自增,马上执行
|
|
||||||
switch (_fsql.Ado.DataType) {
|
|
||||||
case DataType.SqlServer:
|
|
||||||
case DataType.PostgreSQL:
|
|
||||||
DbContextExecCommand();
|
|
||||||
var rets = this.OrmInsert(data).ExecuteInserted();
|
|
||||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
|
||||||
var idx = 0;
|
|
||||||
foreach (var s in data)
|
|
||||||
_fsql.MapEntityValue(rets[idx++], s);
|
|
||||||
IncrAffrows(rets.Count);
|
|
||||||
TrackToList(rets);
|
|
||||||
return;
|
|
||||||
case DataType.MySql:
|
|
||||||
case DataType.Oracle:
|
|
||||||
case DataType.Sqlite:
|
|
||||||
foreach (var s in data)
|
|
||||||
AddPriv(s, false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//进入队列,等待 SaveChanges 时执行
|
|
||||||
foreach (var item in data)
|
|
||||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(item));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Update
|
|
||||||
int DbContextBetchUpdate(EntityState[] ups) => DbContextBetchUpdatePriv(ups, false);
|
|
||||||
int DbContextBetchUpdateNow(EntityState[] ups) => DbContextBetchUpdatePriv(ups, true);
|
|
||||||
int DbContextBetchUpdatePriv(EntityState[] ups, bool isLiveUpdate) {
|
|
||||||
if (ups.Any() == false) return 0;
|
|
||||||
var uplst1 = ups[ups.Length - 1];
|
|
||||||
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
|
|
||||||
|
|
||||||
if (_states.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
|
|
||||||
var lstval2 = default(EntityState);
|
|
||||||
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"特别错误:更新失败,数据未被跟踪:{_fsql.GetEntityString(uplst2.Value)}");
|
|
||||||
|
|
||||||
var cuig1 = _fsql.CompareEntityValueReturnColumns(uplst1.Value, lstval1.Value, true);
|
|
||||||
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(uplst2.Value, lstval2.Value, true) : null;
|
|
||||||
|
|
||||||
List<EntityState> data = null;
|
|
||||||
string[] cuig = null;
|
|
||||||
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
|
|
||||||
//最后一个不保存
|
|
||||||
data = ups.ToList();
|
|
||||||
data.RemoveAt(ups.Length - 1);
|
|
||||||
cuig = cuig2;
|
|
||||||
} else if (isLiveUpdate) {
|
|
||||||
//立即保存
|
|
||||||
data = ups.ToList();
|
|
||||||
cuig = cuig1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data?.Count > 0) {
|
|
||||||
|
|
||||||
if (cuig.Length == _table.Columns.Count)
|
|
||||||
return ups.Length == data.Count ? -998 : -997;
|
|
||||||
|
|
||||||
var updateSource = data.Select(a => a.Value).ToArray();
|
|
||||||
var update = this.OrmUpdate(null).SetSource(updateSource).IgnoreColumns(cuig);
|
|
||||||
|
|
||||||
var affrows = update.ExecuteAffrows();
|
|
||||||
|
|
||||||
foreach (var newval in data) {
|
|
||||||
if (_states.TryGetValue(newval.Key, out var tryold))
|
|
||||||
_fsql.MapEntityValue(newval.Value, tryold.Value);
|
|
||||||
if (newval.OldValue != null)
|
|
||||||
_fsql.MapEntityValue(newval.Value, newval.OldValue);
|
|
||||||
}
|
|
||||||
return affrows;
|
|
||||||
}
|
|
||||||
|
|
||||||
//等待下次对比再保存
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Dictionary<TEntity, byte> _dicUpdateTimes = new Dictionary<TEntity, byte>();
|
|
||||||
public void Update(TEntity data) => UpdateRange(new[] { data });
|
|
||||||
public void UpdateRange(IEnumerable<TEntity> data) {
|
|
||||||
if (CanUpdate(data, true) == false) return;
|
|
||||||
foreach (var item in data) {
|
|
||||||
if (_dicUpdateTimes.ContainsKey(item))
|
|
||||||
DbContextExecCommand();
|
|
||||||
_dicUpdateTimes.Add(item, 1);
|
|
||||||
|
|
||||||
var state = CreateEntityState(item);
|
|
||||||
state.OldValue = item;
|
|
||||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Update, state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Remove
|
|
||||||
int DbContextBetchRemove(EntityState[] dels) {
|
|
||||||
if (dels.Any() == false) return 0;
|
|
||||||
var affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
|
|
||||||
return Math.Max(dels.Length, affrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Remove(TEntity data) => RemoveRange(new[] { data });
|
|
||||||
public void RemoveRange(IEnumerable<TEntity> data) {
|
|
||||||
if (CanRemove(data, true) == false) return;
|
|
||||||
foreach (var item in data) {
|
|
||||||
var state = CreateEntityState(item);
|
|
||||||
if (_states.ContainsKey(state.Key)) _states.Remove(state.Key);
|
|
||||||
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(item);
|
|
||||||
|
|
||||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Delete, state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public static class DbContextDependencyInjection {
|
|
||||||
|
|
||||||
public static IServiceCollection AddFreeDbContext<TDbContext>(this IServiceCollection services, Action<DbContextOptionsBuilder> options) where TDbContext : DbContext {
|
|
||||||
|
|
||||||
services.AddScoped<TDbContext>(sp => {
|
|
||||||
var ctx = Activator.CreateInstance<TDbContext>();
|
|
||||||
|
|
||||||
if (ctx._orm == null) {
|
|
||||||
var builder = new DbContextOptionsBuilder();
|
|
||||||
options(builder);
|
|
||||||
ctx._orm = builder._fsql;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctx;
|
|
||||||
});
|
|
||||||
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
|
||||||
<Version>0.3.27.1</Version>
|
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
|
||||||
<Authors>YeXiangQin</Authors>
|
|
||||||
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
|
||||||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
|
||||||
<PackageTags>FreeSql ORM</PackageTags>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
@ -1,25 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
internal class RepositoryDbContext<TEntity> : DbContext where TEntity : class {
|
|
||||||
|
|
||||||
protected BaseRepository<TEntity> _repos;
|
|
||||||
public RepositoryDbContext(IFreeSql orm, BaseRepository<TEntity> repos) : base() {
|
|
||||||
_orm = orm;
|
|
||||||
_repos = repos;
|
|
||||||
_isUseUnitOfWork = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override object Set(Type entityType) {
|
|
||||||
if (_dicSet.ContainsKey(entityType)) return _dicSet[entityType];
|
|
||||||
var sd = Activator.CreateInstance(typeof(RepositoryDbSet<>).MakeGenericType(entityType), _repos);
|
|
||||||
_dicSet.Add(entityType, sd);
|
|
||||||
return sd;
|
|
||||||
}
|
|
||||||
|
|
||||||
RepositoryDbSet<TEntity> _dbSet;
|
|
||||||
public RepositoryDbSet<TEntity> DbSet => _dbSet ?? (_dbSet = Set<TEntity>() as RepositoryDbSet<TEntity>);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
using FreeSql.Extensions.EntityUtil;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
internal class RepositoryDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
|
||||||
|
|
||||||
protected BaseRepository<TEntity> _repos;
|
|
||||||
public RepositoryDbSet(BaseRepository<TEntity> repos) {
|
|
||||||
_ctx = repos._db;
|
|
||||||
_fsql = repos._fsql;
|
|
||||||
_uow = repos.UnitOfWork;
|
|
||||||
_repos = repos;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override ISelect<TEntity> OrmSelect(object dywhere) {
|
|
||||||
var select = base.OrmSelect(dywhere);
|
|
||||||
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
|
||||||
foreach (var filter in filters) select.Where(filter.Value.Expression);
|
|
||||||
return select.AsTable(_repos.AsTableSelectInternal);
|
|
||||||
}
|
|
||||||
internal ISelect<TEntity> OrmSelectInternal(object dywhere) => OrmSelect(dywhere);
|
|
||||||
protected override IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys) {
|
|
||||||
var update = base.OrmUpdate(entitys);
|
|
||||||
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
|
||||||
foreach (var filter in filters) {
|
|
||||||
if (entitys != null)
|
|
||||||
foreach (var entity in entitys)
|
|
||||||
if (filter.Value.ExpressionDelegate?.Invoke(entity) == false)
|
|
||||||
throw new Exception($"FreeSql.Repository Update 失败,因为设置了过滤器 {filter.Key}: {filter.Value.Expression},更新的数据不符合 {_fsql.GetEntityString(entity)}");
|
|
||||||
update.Where(filter.Value.Expression);
|
|
||||||
}
|
|
||||||
return update.AsTable(_repos.AsTableInternal);
|
|
||||||
}
|
|
||||||
internal IUpdate<TEntity> OrmUpdateInternal(IEnumerable<TEntity> entitys) => OrmUpdate(entitys);
|
|
||||||
protected override IDelete<TEntity> OrmDelete(object dywhere) {
|
|
||||||
var delete = base.OrmDelete(dywhere);
|
|
||||||
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
|
||||||
foreach (var filter in filters) delete.Where(filter.Value.Expression);
|
|
||||||
return delete.AsTable(_repos.AsTableInternal);
|
|
||||||
}
|
|
||||||
internal IDelete<TEntity> OrmDeleteInternal(object dywhere) => OrmDelete(dywhere);
|
|
||||||
protected override IInsert<TEntity> OrmInsert(TEntity entity) => OrmInsert(new[] { entity });
|
|
||||||
protected override IInsert<TEntity> OrmInsert(IEnumerable<TEntity> entitys) {
|
|
||||||
var insert = base.OrmInsert(entitys);
|
|
||||||
var filters = (_repos.DataFilter as DataFilter<TEntity>)._filters.Where(a => a.Value.IsEnabled == true);
|
|
||||||
foreach (var filter in filters) {
|
|
||||||
if (entitys != null)
|
|
||||||
foreach (var entity in entitys)
|
|
||||||
if (filter.Value.ExpressionDelegate?.Invoke(entity) == false)
|
|
||||||
throw new Exception($"FreeSql.Repository Insert 失败,因为设置了过滤器 {filter.Key}: {filter.Value.Expression},插入的数据不符合 {_fsql.GetEntityString(entity)}");
|
|
||||||
}
|
|
||||||
return insert.AsTable(_repos.AsTableInternal);
|
|
||||||
}
|
|
||||||
internal IInsert<TEntity> OrmInsertInternal(TEntity entity) => OrmInsert(entity);
|
|
||||||
internal IInsert<TEntity> OrmInsertInternal(IEnumerable<TEntity> entitys) => OrmInsert(entitys);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
|
|
||||||
public interface IRepositoryUnitOfWork : IUnitOfWork {
|
|
||||||
/// <summary>
|
|
||||||
/// 在工作单元内创建默认仓库类,工作单元下的仓储操作具有事务特点
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <typeparam name="TKey"></typeparam>
|
|
||||||
/// <param name="filter">数据过滤 + 验证</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 在工作单元内创建仓库类,工作单元下的仓储操作具有事务特点
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <param name="filter">数据过滤 + 验证</param>
|
|
||||||
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class;
|
|
||||||
}
|
|
||||||
|
|
||||||
class RepositoryUnitOfWork : UnitOfWork, IRepositoryUnitOfWork {
|
|
||||||
|
|
||||||
public RepositoryUnitOfWork(IFreeSql fsql) : base(fsql) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
|
|
||||||
var repos = new GuidRepository<TEntity>(_fsql, filter, asTable);
|
|
||||||
repos.UnitOfWork = this;
|
|
||||||
return repos;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
|
|
||||||
var repos = new DefaultRepository<TEntity, TKey>(_fsql, filter);
|
|
||||||
repos.UnitOfWork = this;
|
|
||||||
return repos;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,153 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Text;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public interface IDataFilter<TEntity> : IDisposable where TEntity : class {
|
|
||||||
|
|
||||||
IDataFilter<TEntity> Apply(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 开启过滤器,若使用 using 则使用完后,恢复为原有状态
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="filterName">过滤器名称</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
IDisposable Enable(params string[] filterName);
|
|
||||||
/// <summary>
|
|
||||||
/// 开启所有过滤器,若使用 using 则使用完后,恢复为原有状态
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
IDisposable EnableAll();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 禁用过滤器,若使用 using 则使用完后,恢复为原有状态
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="filterName"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
IDisposable Disable(params string[] filterName);
|
|
||||||
/// <summary>
|
|
||||||
/// 禁用所有过滤器,若使用 using 则使用完后,恢复为原有状态
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
IDisposable DisableAll();
|
|
||||||
|
|
||||||
bool IsEnabled(string filterName);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class DataFilter<TEntity> : IDataFilter<TEntity> where TEntity : class {
|
|
||||||
|
|
||||||
internal class FilterItem {
|
|
||||||
public Expression<Func<TEntity, bool>> Expression { get; set; }
|
|
||||||
Func<TEntity, bool> _expressionDelegate;
|
|
||||||
public Func<TEntity, bool> ExpressionDelegate => _expressionDelegate ?? (_expressionDelegate = Expression?.Compile());
|
|
||||||
public bool IsEnabled { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
internal ConcurrentDictionary<string, FilterItem> _filters = new ConcurrentDictionary<string, FilterItem>(StringComparer.CurrentCultureIgnoreCase);
|
|
||||||
public IDataFilter<TEntity> Apply(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp) {
|
|
||||||
|
|
||||||
if (filterName == null)
|
|
||||||
throw new ArgumentNullException(nameof(filterName));
|
|
||||||
if (filterAndValidateExp == null) return this;
|
|
||||||
|
|
||||||
var filterItem = new FilterItem { Expression = filterAndValidateExp, IsEnabled = true };
|
|
||||||
_filters.AddOrUpdate(filterName, filterItem, (k, v) => filterItem);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public IDisposable Disable(params string[] filterName) {
|
|
||||||
if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });
|
|
||||||
|
|
||||||
List<string> restore = new List<string>();
|
|
||||||
foreach (var name in filterName) {
|
|
||||||
if (_filters.TryGetValue(name, out var tryfi)) {
|
|
||||||
if (tryfi.IsEnabled) {
|
|
||||||
restore.Add(name);
|
|
||||||
tryfi.IsEnabled = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new UsingAny(() => this.Enable(restore.ToArray()));
|
|
||||||
}
|
|
||||||
public IDisposable DisableAll() {
|
|
||||||
List<string> restore = new List<string>();
|
|
||||||
foreach (var val in _filters) {
|
|
||||||
if (val.Value.IsEnabled) {
|
|
||||||
restore.Add(val.Key);
|
|
||||||
val.Value.IsEnabled = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new UsingAny(() => this.Enable(restore.ToArray()));
|
|
||||||
}
|
|
||||||
class UsingAny : IDisposable {
|
|
||||||
Action _ondis;
|
|
||||||
public UsingAny(Action ondis) {
|
|
||||||
_ondis = ondis;
|
|
||||||
}
|
|
||||||
public void Dispose() {
|
|
||||||
_ondis?.Invoke();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IDisposable Enable(params string[] filterName) {
|
|
||||||
if (filterName == null || filterName.Any() == false) return new UsingAny(() => { });
|
|
||||||
|
|
||||||
List<string> restore = new List<string>();
|
|
||||||
foreach (var name in filterName) {
|
|
||||||
if (_filters.TryGetValue(name, out var tryfi)) {
|
|
||||||
if (tryfi.IsEnabled == false) {
|
|
||||||
restore.Add(name);
|
|
||||||
tryfi.IsEnabled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new UsingAny(() => this.Disable(restore.ToArray()));
|
|
||||||
}
|
|
||||||
public IDisposable EnableAll() {
|
|
||||||
List<string> restore = new List<string>();
|
|
||||||
foreach (var val in _filters) {
|
|
||||||
if (val.Value.IsEnabled == false) {
|
|
||||||
restore.Add(val.Key);
|
|
||||||
val.Value.IsEnabled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new UsingAny(() => this.Disable(restore.ToArray()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsEnabled(string filterName) {
|
|
||||||
if (filterName == null) return false;
|
|
||||||
return _filters.TryGetValue(filterName, out var tryfi) ? tryfi.IsEnabled : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
~DataFilter() {
|
|
||||||
this.Dispose();
|
|
||||||
}
|
|
||||||
public void Dispose() {
|
|
||||||
_filters.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class FluentDataFilter : IDisposable {
|
|
||||||
|
|
||||||
internal List<(Type type, string name, LambdaExpression exp)> _filters = new List<(Type type, string name, LambdaExpression exp)>();
|
|
||||||
|
|
||||||
public FluentDataFilter Apply<TEntity>(string filterName, Expression<Func<TEntity, bool>> filterAndValidateExp) where TEntity : class {
|
|
||||||
if (filterName == null)
|
|
||||||
throw new ArgumentNullException(nameof(filterName));
|
|
||||||
if (filterAndValidateExp == null) return this;
|
|
||||||
|
|
||||||
_filters.Add((typeof(TEntity), filterName, filterAndValidateExp));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
~FluentDataFilter() {
|
|
||||||
this.Dispose();
|
|
||||||
}
|
|
||||||
public void Dispose() {
|
|
||||||
_filters.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,90 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
|
|
||||||
internal class DataFilterUtil {
|
|
||||||
|
|
||||||
internal static Action<FluentDataFilter> _globalDataFilter;
|
|
||||||
|
|
||||||
static ConcurrentDictionary<Type, Delegate> _dicSetRepositoryDataFilterApplyDataFilterFunc = new ConcurrentDictionary<Type, Delegate>();
|
|
||||||
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicSetRepositoryDataFilterConvertFilterNotExists = new ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>();
|
|
||||||
internal static void SetRepositoryDataFilter(object repos, Action<FluentDataFilter> scopedDataFilter) {
|
|
||||||
if (scopedDataFilter != null) {
|
|
||||||
SetRepositoryDataFilter(repos, null);
|
|
||||||
}
|
|
||||||
if (scopedDataFilter == null) {
|
|
||||||
scopedDataFilter = _globalDataFilter;
|
|
||||||
}
|
|
||||||
if (scopedDataFilter == null) return;
|
|
||||||
using (var globalFilter = new FluentDataFilter()) {
|
|
||||||
scopedDataFilter(globalFilter);
|
|
||||||
|
|
||||||
var type = repos.GetType();
|
|
||||||
Type entityType = (repos as IRepository).EntityType;
|
|
||||||
if (entityType == null) throw new Exception("FreeSql.Repository 设置过滤器失败,原因是对象不属于 IRepository");
|
|
||||||
|
|
||||||
var notExists = _dicSetRepositoryDataFilterConvertFilterNotExists.GetOrAdd(type, t => new ConcurrentDictionary<string, bool>());
|
|
||||||
var newFilter = new Dictionary<string, LambdaExpression>();
|
|
||||||
foreach (var gf in globalFilter._filters) {
|
|
||||||
if (notExists.ContainsKey(gf.name)) continue;
|
|
||||||
|
|
||||||
LambdaExpression newExp = null;
|
|
||||||
var filterParameter1 = Expression.Parameter(entityType, gf.exp.Parameters[0].Name);
|
|
||||||
try {
|
|
||||||
newExp = Expression.Lambda(
|
|
||||||
typeof(Func<,>).MakeGenericType(entityType, typeof(bool)),
|
|
||||||
new ReplaceVisitor().Modify(gf.exp.Body, filterParameter1),
|
|
||||||
filterParameter1
|
|
||||||
);
|
|
||||||
} catch {
|
|
||||||
notExists.TryAdd(gf.name, true); //防止第二次错误
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
newFilter.Add(gf.name, newExp);
|
|
||||||
}
|
|
||||||
if (newFilter.Any() == false) return;
|
|
||||||
|
|
||||||
var del = _dicSetRepositoryDataFilterApplyDataFilterFunc.GetOrAdd(type, t => {
|
|
||||||
var reposParameter = Expression.Parameter(type);
|
|
||||||
var nameParameter = Expression.Parameter(typeof(string));
|
|
||||||
var expressionParameter = Expression.Parameter(
|
|
||||||
typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(entityType, typeof(bool)))
|
|
||||||
);
|
|
||||||
return Expression.Lambda(
|
|
||||||
Expression.Block(
|
|
||||||
Expression.Call(reposParameter, type.GetMethod("ApplyDataFilter", BindingFlags.Instance | BindingFlags.NonPublic), nameParameter, expressionParameter)
|
|
||||||
),
|
|
||||||
new[] {
|
|
||||||
reposParameter, nameParameter, expressionParameter
|
|
||||||
}
|
|
||||||
).Compile();
|
|
||||||
});
|
|
||||||
foreach (var nf in newFilter) {
|
|
||||||
del.DynamicInvoke(repos, nf.Key, nf.Value);
|
|
||||||
}
|
|
||||||
newFilter.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ReplaceVisitor : ExpressionVisitor {
|
|
||||||
private ParameterExpression parameter;
|
|
||||||
|
|
||||||
public Expression Modify(Expression expression, ParameterExpression parameter) {
|
|
||||||
this.parameter = parameter;
|
|
||||||
return Visit(expression);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override Expression VisitMember(MemberExpression node) {
|
|
||||||
if (node.Expression?.NodeType == ExpressionType.Parameter)
|
|
||||||
return Expression.Property(parameter, node.Member.Name);
|
|
||||||
return base.VisitMember(node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
using FreeSql;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Linq;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public static class FreeSqlRepositoryDependencyInjection {
|
|
||||||
|
|
||||||
public static IServiceCollection AddFreeRepository(this IServiceCollection services, Action<FluentDataFilter> globalDataFilter = null, params Assembly[] assemblies) {
|
|
||||||
|
|
||||||
DataFilterUtil._globalDataFilter = globalDataFilter;
|
|
||||||
|
|
||||||
services.AddScoped(typeof(IReadOnlyRepository<>), typeof(GuidRepository<>));
|
|
||||||
services.AddScoped(typeof(IBasicRepository<>), typeof(GuidRepository<>));
|
|
||||||
services.AddScoped(typeof(BaseRepository<>), typeof(GuidRepository<>));
|
|
||||||
services.AddScoped(typeof(GuidRepository<>));
|
|
||||||
|
|
||||||
services.AddScoped(typeof(IReadOnlyRepository<,>), typeof(DefaultRepository<,>));
|
|
||||||
services.AddScoped(typeof(IBasicRepository<,>), typeof(DefaultRepository<,>));
|
|
||||||
services.AddScoped(typeof(BaseRepository<,>), typeof(DefaultRepository<,>));
|
|
||||||
services.AddScoped(typeof(DefaultRepository<,>));
|
|
||||||
|
|
||||||
if (assemblies?.Any() == true) {
|
|
||||||
foreach(var asse in assemblies) {
|
|
||||||
foreach (var repos in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(IRepository).IsAssignableFrom(a))) {
|
|
||||||
|
|
||||||
services.AddScoped(repos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,58 +0,0 @@
|
|||||||
using FreeSql;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Text;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Data;
|
|
||||||
|
|
||||||
public static class FreeSqlRepositoryExtenssions {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 返回默认仓库类
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <typeparam name="TKey"></typeparam>
|
|
||||||
/// <param name="that"></param>
|
|
||||||
/// <param name="filter">数据过滤 + 验证</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
|
|
||||||
return new DefaultRepository<TEntity, TKey>(that, filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 返回仓库类
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <param name="that"></param>
|
|
||||||
/// <param name="filter">数据过滤 + 验证</param>
|
|
||||||
/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static GuidRepository<TEntity> GetGuidRepository<TEntity>(this IFreeSql that, Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
|
|
||||||
return new GuidRepository<TEntity>(that, filter, asTable);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 合并两个仓储的设置(过滤+分表),以便查询
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="TEntity"></typeparam>
|
|
||||||
/// <typeparam name="T2"></typeparam>
|
|
||||||
/// <param name="that"></param>
|
|
||||||
/// <param name="repos"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static ISelect<TEntity> FromRepository<TEntity, T2>(this ISelect<TEntity> that, BaseRepository<T2> repos) where TEntity : class where T2 : class {
|
|
||||||
var filters = (repos.DataFilter as DataFilter<T2>)._filters.Where(a => a.Value.IsEnabled == true);
|
|
||||||
foreach (var filter in filters) that.Where<T2>(filter.Value.Expression);
|
|
||||||
return that.AsTable(repos.AsTableSelectInternal);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 创建基于仓储功能的工作单元,务必使用 using 包含使用
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="that"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IRepositoryUnitOfWork CreateUnitOfWork(this IFreeSql that) {
|
|
||||||
return new RepositoryUnitOfWork(that);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,143 +0,0 @@
|
|||||||
using FreeSql.Extensions.EntityUtil;
|
|
||||||
using FreeSql.Internal.Model;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public abstract class BaseRepository<TEntity> : IRepository<TEntity>
|
|
||||||
where TEntity : class {
|
|
||||||
|
|
||||||
internal IFreeSql _fsql;
|
|
||||||
internal RepositoryDbContext<TEntity> _db;
|
|
||||||
public IDataFilter<TEntity> DataFilter { get; } = new DataFilter<TEntity>();
|
|
||||||
Func<string, string> _asTableVal;
|
|
||||||
protected Func<string, string> AsTable {
|
|
||||||
get => _asTableVal;
|
|
||||||
set {
|
|
||||||
_asTableVal = value;
|
|
||||||
AsTableSelect = value == null ? null : new Func<Type, string, string>((a, b) => a == EntityType ? value(b) : null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
internal Func<string, string> AsTableInternal => AsTable;
|
|
||||||
protected Func<Type, string, string> AsTableSelect { get; private set; }
|
|
||||||
internal Func<Type, string, string> AsTableSelectInternal => AsTableSelect;
|
|
||||||
|
|
||||||
protected BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) {
|
|
||||||
_fsql = fsql;
|
|
||||||
DataFilterUtil.SetRepositoryDataFilter(this, null);
|
|
||||||
DataFilter.Apply("", filter);
|
|
||||||
AsTable = asTable;
|
|
||||||
_db = new RepositoryDbContext<TEntity>(_fsql, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
~BaseRepository() {
|
|
||||||
this.Dispose();
|
|
||||||
}
|
|
||||||
bool _isdisposed = false;
|
|
||||||
public void Dispose() {
|
|
||||||
if (_isdisposed) return;
|
|
||||||
try {
|
|
||||||
_db.Dispose();
|
|
||||||
this.DataFilter.Dispose();
|
|
||||||
} finally {
|
|
||||||
_isdisposed = true;
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IUnitOfWork UnitOfWork { get; set; }
|
|
||||||
public Type EntityType => _db.DbSet._entityTypeInternal;
|
|
||||||
public IUpdate<TEntity> UpdateDiy => _db.DbSet.OrmUpdateInternal(null);
|
|
||||||
|
|
||||||
public ISelect<TEntity> Select => _db.DbSet.OrmSelectInternal(null);
|
|
||||||
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => _db.DbSet.OrmSelectInternal(null).Where(exp);
|
|
||||||
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => _db.DbSet.OrmSelectInternal(null).WhereIf(condition, exp);
|
|
||||||
|
|
||||||
public int Delete(Expression<Func<TEntity, bool>> predicate) => _db.DbSet.OrmDeleteInternal(null).Where(predicate).ExecuteAffrows();
|
|
||||||
public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate) => _db.DbSet.OrmDeleteInternal(null).Where(predicate).ExecuteAffrowsAsync();
|
|
||||||
|
|
||||||
public int Delete(TEntity entity) {
|
|
||||||
_db.DbSet.Remove(entity);
|
|
||||||
return _db.SaveChanges();
|
|
||||||
}
|
|
||||||
public Task<int> DeleteAsync(TEntity entity) {
|
|
||||||
_db.DbSet.Remove(entity);
|
|
||||||
return _db.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
public int Delete(IEnumerable<TEntity> entitys) {
|
|
||||||
_db.DbSet.RemoveRange(entitys);
|
|
||||||
return _db.SaveChanges();
|
|
||||||
}
|
|
||||||
public Task<int> DeleteAsync(IEnumerable<TEntity> entitys) {
|
|
||||||
_db.DbSet.RemoveRange(entitys);
|
|
||||||
return _db.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual TEntity Insert(TEntity entity) {
|
|
||||||
_db.DbSet.Add(entity);
|
|
||||||
_db.SaveChanges();
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
async public virtual Task<TEntity> InsertAsync(TEntity entity) {
|
|
||||||
await _db.DbSet.AddAsync(entity);
|
|
||||||
_db.SaveChanges();
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
public virtual List<TEntity> Insert(IEnumerable<TEntity> entitys) {
|
|
||||||
_db.DbSet.AddRange(entitys);
|
|
||||||
_db.SaveChanges();
|
|
||||||
return entitys.ToList();
|
|
||||||
}
|
|
||||||
async public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys) {
|
|
||||||
await _db.DbSet.AddRangeAsync(entitys);
|
|
||||||
await _db.SaveChangesAsync();
|
|
||||||
return entitys.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Update(TEntity entity) {
|
|
||||||
_db.DbSet.Update(entity);
|
|
||||||
return _db.SaveChanges();
|
|
||||||
}
|
|
||||||
public Task<int> UpdateAsync(TEntity entity) {
|
|
||||||
_db.DbSet.Update(entity);
|
|
||||||
return _db.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
public int Update(IEnumerable<TEntity> entitys) {
|
|
||||||
_db.DbSet.UpdateRange(entitys);
|
|
||||||
return _db.SaveChanges();
|
|
||||||
}
|
|
||||||
public Task<int> UpdateAsync(IEnumerable<TEntity> entitys) {
|
|
||||||
_db.DbSet.UpdateRange(entitys);
|
|
||||||
return _db.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract class BaseRepository<TEntity, TKey> : BaseRepository<TEntity>, IRepository<TEntity, TKey>
|
|
||||||
where TEntity : class {
|
|
||||||
|
|
||||||
public BaseRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable = null) : base(fsql, filter, asTable) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Delete(TKey id) {
|
|
||||||
var stateKey = string.Concat(id);
|
|
||||||
if (_db.DbSet._statesInternal.ContainsKey(stateKey))
|
|
||||||
_db.DbSet._statesInternal.Remove(stateKey);
|
|
||||||
return _db.DbSet.OrmDeleteInternal(id).ExecuteAffrows();
|
|
||||||
}
|
|
||||||
public Task<int> DeleteAsync(TKey id) {
|
|
||||||
var stateKey = string.Concat(id);
|
|
||||||
if (_db.DbSet._statesInternal.ContainsKey(stateKey))
|
|
||||||
_db.DbSet._statesInternal.Remove(stateKey);
|
|
||||||
return _db.DbSet.OrmDeleteInternal(id).ExecuteAffrowsAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public TEntity Find(TKey id) => _db.DbSet.OrmSelectInternal(id).ToOne();
|
|
||||||
public Task<TEntity> FindAsync(TKey id) => _db.DbSet.OrmSelectInternal(id).ToOneAsync();
|
|
||||||
|
|
||||||
public TEntity Get(TKey id) => Find(id);
|
|
||||||
public Task<TEntity> GetAsync(TKey id) => FindAsync(id);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public class DefaultRepository<TEntity, TKey> :
|
|
||||||
BaseRepository<TEntity, TKey>
|
|
||||||
where TEntity : class {
|
|
||||||
|
|
||||||
public DefaultRepository(IFreeSql fsql) : base(fsql, null, null) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter, null) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public class GuidRepository<TEntity> :
|
|
||||||
BaseRepository<TEntity, Guid>
|
|
||||||
where TEntity : class {
|
|
||||||
|
|
||||||
public GuidRepository(IFreeSql fsql) : this(fsql, null, null) {
|
|
||||||
|
|
||||||
}
|
|
||||||
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) : base(fsql, filter, asTable) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,32 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public interface IBasicRepository<TEntity> : IReadOnlyRepository<TEntity>
|
|
||||||
where TEntity : class {
|
|
||||||
TEntity Insert(TEntity entity);
|
|
||||||
List<TEntity> Insert(IEnumerable<TEntity> entitys);
|
|
||||||
Task<TEntity> InsertAsync(TEntity entity);
|
|
||||||
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys);
|
|
||||||
|
|
||||||
int Update(TEntity entity);
|
|
||||||
int Update(IEnumerable<TEntity> entitys);
|
|
||||||
Task<int> UpdateAsync(TEntity entity);
|
|
||||||
Task<int> UpdateAsync(IEnumerable<TEntity> entitys);
|
|
||||||
|
|
||||||
IUpdate<TEntity> UpdateDiy { get; }
|
|
||||||
|
|
||||||
int Delete(TEntity entity);
|
|
||||||
int Delete(IEnumerable<TEntity> entitys);
|
|
||||||
Task<int> DeleteAsync(TEntity entity);
|
|
||||||
Task<int> DeleteAsync(IEnumerable<TEntity> entitys);
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface IBasicRepository<TEntity, TKey> : IBasicRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>
|
|
||||||
where TEntity : class {
|
|
||||||
int Delete(TKey id);
|
|
||||||
|
|
||||||
Task<int> DeleteAsync(TKey id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public interface IReadOnlyRepository<TEntity> : IRepository
|
|
||||||
where TEntity : class {
|
|
||||||
|
|
||||||
IDataFilter<TEntity> DataFilter { get; }
|
|
||||||
|
|
||||||
ISelect<TEntity> Select { get; }
|
|
||||||
|
|
||||||
ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp);
|
|
||||||
ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp);
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface IReadOnlyRepository<TEntity, TKey> : IReadOnlyRepository<TEntity>
|
|
||||||
where TEntity : class {
|
|
||||||
TEntity Get(TKey id);
|
|
||||||
|
|
||||||
Task<TEntity> GetAsync(TKey id);
|
|
||||||
|
|
||||||
TEntity Find(TKey id);
|
|
||||||
|
|
||||||
Task<TEntity> FindAsync(TKey id);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
|
|
||||||
public interface IRepository : IDisposable {
|
|
||||||
Type EntityType { get; }
|
|
||||||
IUnitOfWork UnitOfWork { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface IRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepository<TEntity>
|
|
||||||
where TEntity : class {
|
|
||||||
int Delete(Expression<Func<TEntity, bool>> predicate);
|
|
||||||
|
|
||||||
Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate);
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface IRepository<TEntity, TKey> : IRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>, IBasicRepository<TEntity, TKey>
|
|
||||||
where TEntity : class {
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
public interface IUnitOfWork : IDisposable {
|
|
||||||
|
|
||||||
DbTransaction GetOrBeginTransaction(bool isCreate = true);
|
|
||||||
|
|
||||||
void Commit();
|
|
||||||
|
|
||||||
void Rollback();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,73 +0,0 @@
|
|||||||
using SafeObjectPool;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace FreeSql {
|
|
||||||
class UnitOfWork : IUnitOfWork {
|
|
||||||
|
|
||||||
protected IFreeSql _fsql;
|
|
||||||
protected Object<DbConnection> _conn;
|
|
||||||
protected DbTransaction _tran;
|
|
||||||
|
|
||||||
public UnitOfWork(IFreeSql fsql) {
|
|
||||||
_fsql = fsql;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ReturnObject() {
|
|
||||||
_fsql.Ado.MasterPool.Return(_conn);
|
|
||||||
_tran = null;
|
|
||||||
_conn = null;
|
|
||||||
}
|
|
||||||
public DbTransaction GetOrBeginTransaction(bool isCreate = true) {
|
|
||||||
|
|
||||||
if (_tran != null) return _tran;
|
|
||||||
if (isCreate == false) return null;
|
|
||||||
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
|
|
||||||
|
|
||||||
_conn = _fsql.Ado.MasterPool.Get();
|
|
||||||
try {
|
|
||||||
_tran = _conn.Value.BeginTransaction();
|
|
||||||
} catch {
|
|
||||||
ReturnObject();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
return _tran;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Commit() {
|
|
||||||
if (_tran != null) {
|
|
||||||
try {
|
|
||||||
_tran.Commit();
|
|
||||||
} finally {
|
|
||||||
ReturnObject();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public void Rollback() {
|
|
||||||
if (_tran != null) {
|
|
||||||
try {
|
|
||||||
_tran.Rollback();
|
|
||||||
} finally {
|
|
||||||
ReturnObject();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
~UnitOfWork() {
|
|
||||||
this.Dispose();
|
|
||||||
}
|
|
||||||
bool _isdisposed = false;
|
|
||||||
public void Dispose() {
|
|
||||||
if (_isdisposed) return;
|
|
||||||
try {
|
|
||||||
this.Rollback();
|
|
||||||
} finally {
|
|
||||||
_isdisposed = true;
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -13,7 +13,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
|
<ProjectReference Include="..\FreeSql.Repository\FreeSql.Repository.csproj" />
|
||||||
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -29,17 +29,6 @@ namespace FreeSql.Tests {
|
|||||||
|
|
||||||
ISelect<TestInfo> select => g.mysql.Select<TestInfo>();
|
ISelect<TestInfo> select => g.mysql.Select<TestInfo>();
|
||||||
|
|
||||||
|
|
||||||
class OrderContext : DbContext {
|
|
||||||
|
|
||||||
public DbSet<Order> Orders { get; set; }
|
|
||||||
public DbSet<OrderDetail> OrderDetails { get; set; }
|
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder builder) {
|
|
||||||
builder.UseFreeSql(g.mysql);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TestUser {
|
class TestUser {
|
||||||
[Column(IsIdentity = true)]
|
[Column(IsIdentity = true)]
|
||||||
public int stringid { get; set; }
|
public int stringid { get; set; }
|
||||||
@ -59,16 +48,7 @@ namespace FreeSql.Tests {
|
|||||||
.InnerJoin(a => a.LogOn.id == a.stringid).ToSql();
|
.InnerJoin(a => a.LogOn.id == a.stringid).ToSql();
|
||||||
|
|
||||||
|
|
||||||
using (var ctx = new OrderContext()) {
|
var parentSelect1 = select.Where(a => a.Type.Parent.Parent.Parent.Parent.Name == "").Where(b => b.Type.Name == "").ToSql();
|
||||||
//ctx.Orders.OrmInsert(new Order { }).ExecuteAffrows();
|
|
||||||
//ctx.Orders.OrmDelete.Where(a => a.Id > 0).ExecuteAffrows();
|
|
||||||
|
|
||||||
//ctx.OrderDetails.OrmSelect.Where(dt => dt.Order.Id == 10).ToList();
|
|
||||||
|
|
||||||
ctx.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
var parentSelect1 = select.Where(a => a.Type.Parent.Parent.Parent.Parent.Name == "").Where(b => b.Type.Name == "").ToSql();
|
|
||||||
|
|
||||||
|
|
||||||
var collSelect1 = g.mysql.Select<Order>().Where(a =>
|
var collSelect1 = g.mysql.Select<Order>().Where(a =>
|
||||||
|
31
FreeSql.sln
31
FreeSql.sln
@ -30,11 +30,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "domain_01", "Examples\domai
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "net461_console_01", "Examples\net461_console_01\net461_console_01.csproj", "{0637A778-338E-4096-B439-32B18306C75F}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "net461_console_01", "Examples\net461_console_01\net461_console_01.csproj", "{0637A778-338E-4096-B439-32B18306C75F}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "orm_vs", "Examples\orm_vs\orm_vs.csproj", "{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "orm_vs", "Examples\orm_vs\orm_vs.csproj", "{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}"
|
||||||
EndProject
|
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FreeSql.DbContext", "FreeSql.DbContext\FreeSql.DbContext.csproj", "{E2D20A95-3045-49BD-973C-0CC6CEB957DB}"
|
|
||||||
EndProject
|
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dbcontext_01", "Examples\dbcontext_01\dbcontext_01.csproj", "{9ED752FF-F908-4611-827A-E99655607567}"
|
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@ -178,30 +174,6 @@ Global
|
|||||||
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x64.Build.0 = Release|Any CPU
|
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x86.ActiveCfg = Release|Any CPU
|
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x86.Build.0 = Release|Any CPU
|
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81}.Release|x86.Build.0 = Release|Any CPU
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|x64.ActiveCfg = Debug|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|x64.Build.0 = Debug|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|x86.ActiveCfg = Debug|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Debug|x86.Build.0 = Debug|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|x64.ActiveCfg = Release|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|x64.Build.0 = Release|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|x86.ActiveCfg = Release|Any CPU
|
|
||||||
{E2D20A95-3045-49BD-973C-0CC6CEB957DB}.Release|x86.Build.0 = Release|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Debug|x64.ActiveCfg = Debug|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Debug|x64.Build.0 = Debug|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Debug|x86.ActiveCfg = Debug|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Debug|x86.Build.0 = Debug|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Release|x64.ActiveCfg = Release|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Release|x64.Build.0 = Release|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Release|x86.ActiveCfg = Release|Any CPU
|
|
||||||
{9ED752FF-F908-4611-827A-E99655607567}.Release|x86.Build.0 = Release|Any CPU
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@ -213,7 +185,6 @@ Global
|
|||||||
{A23D0455-CA7B-442D-827E-C4C7E84F9084} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
{A23D0455-CA7B-442D-827E-C4C7E84F9084} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
||||||
{0637A778-338E-4096-B439-32B18306C75F} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
{0637A778-338E-4096-B439-32B18306C75F} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
||||||
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
{1A5EC2EB-8C2B-4547-8AC6-EB5C0DE0CA81} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
||||||
{9ED752FF-F908-4611-827A-E99655607567} = {94C8A78D-AA15-47B2-A348-530CD86BFC1B}
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {089687FD-5D25-40AB-BA8A-A10D1E137F98}
|
SolutionGuid = {089687FD-5D25-40AB-BA8A-A10D1E137F98}
|
||||||
|
@ -304,6 +304,37 @@ namespace FreeSql.Extensions.EntityUtil {
|
|||||||
func(item);
|
func(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object>>> _dicClearEntityPrimaryValueWithIdentity = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object>>>();
|
||||||
|
/// <summary>
|
||||||
|
/// 清除实体的主键值,将自增、Guid类型的主键值清除
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity"></typeparam>
|
||||||
|
/// <param name="orm"></param>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
public static void ClearEntityPrimaryValueWithIdentity<TEntity>(this IFreeSql orm, TEntity item) {
|
||||||
|
var func = _dicClearEntityPrimaryValueWithIdentity.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object>>()).GetOrAdd(typeof(TEntity), t => {
|
||||||
|
var _table = orm.CodeFirst.GetTableByEntity(t);
|
||||||
|
var identitys = _table.Primarys.Where(a => a.Attribute.IsIdentity);
|
||||||
|
var parm1 = Expression.Parameter(typeof(object));
|
||||||
|
var var1Parm = Expression.Variable(t);
|
||||||
|
var exps = new List<Expression>(new Expression[] {
|
||||||
|
Expression.Assign(var1Parm, Expression.TypeAs(parm1, t))
|
||||||
|
});
|
||||||
|
foreach (var pk in _table.Primarys) {
|
||||||
|
if (pk.Attribute.IsIdentity) {
|
||||||
|
exps.Add(
|
||||||
|
Expression.Assign(
|
||||||
|
Expression.MakeMemberAccess(var1Parm, _table.Properties[pk.CsName]),
|
||||||
|
Expression.Default(pk.CsType)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Expression.Lambda<Action<object>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1 }).Compile();
|
||||||
|
});
|
||||||
|
func(item);
|
||||||
|
}
|
||||||
|
|
||||||
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object, bool, string[]>>> _dicCompareEntityValueReturnColumns = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object, bool, string[]>>>();
|
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object, bool, string[]>>> _dicCompareEntityValueReturnColumns = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Func<object, object, bool, string[]>>>();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 对比两个实体值,返回相同/或不相同的列名
|
/// 对比两个实体值,返回相同/或不相同的列名
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
|
||||||
<Version>0.3.14</Version>
|
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
|
||||||
<Authors>YeXiangQin</Authors>
|
|
||||||
<Description>打造 .NETCore 最方便的 ORM,DbFirst 与 CodeFirst 混合使用,提供从实体同步数据库,或者从数据库生成实体代码,支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite,读写分离、分表分库。</Description>
|
|
||||||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
|
||||||
<PackageTags>FreeSql ORM</PackageTags>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Compile Remove="Oracle\**" />
|
|
||||||
<Compile Remove="PostgreSQL\**" />
|
|
||||||
<Compile Remove="Sqlite\**" />
|
|
||||||
<Compile Remove="SqlServer\**" />
|
|
||||||
<EmbeddedResource Remove="Oracle\**" />
|
|
||||||
<EmbeddedResource Remove="PostgreSQL\**" />
|
|
||||||
<EmbeddedResource Remove="Sqlite\**" />
|
|
||||||
<EmbeddedResource Remove="SqlServer\**" />
|
|
||||||
<None Remove="Oracle\**" />
|
|
||||||
<None Remove="PostgreSQL\**" />
|
|
||||||
<None Remove="Sqlite\**" />
|
|
||||||
<None Remove="SqlServer\**" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Compile Remove="FreeSqlBuilder.cs" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="CS-Script.Core" Version="1.0.6" />
|
|
||||||
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.1.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.0" />
|
|
||||||
<PackageReference Include="MySql.Data" Version="8.0.15" />
|
|
||||||
<PackageReference Include="Npgsql.LegacyPostgis" Version="4.0.5" />
|
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
|
|
||||||
<PackageReference Include="SafeObjectPool" Version="2.0.1" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<Version>0.3.27</Version>
|
<Version>0.4.1</Version>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
<Authors>YeXiangQin</Authors>
|
<Authors>YeXiangQin</Authors>
|
||||||
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user