mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
## v0.11.18
This commit is contained in:
@ -92,6 +92,7 @@ namespace FreeSql
|
||||
|
||||
protected List<IDbSet> _listSet = new List<IDbSet>();
|
||||
protected Dictionary<Type, IDbSet> _dicSet = new Dictionary<Type, IDbSet>();
|
||||
internal Dictionary<Type, IDbSet> InternalDicSet => _dicSet;
|
||||
public DbSet<TEntity> Set<TEntity>() where TEntity : class => this.Set(typeof(TEntity)) as DbSet<TEntity>;
|
||||
public virtual IDbSet Set(Type entityType)
|
||||
{
|
||||
@ -105,12 +106,21 @@ namespace FreeSql
|
||||
#endregion
|
||||
|
||||
#region DbSet 快速代理
|
||||
void CheckEntityTypeOrThrow(Type entityType)
|
||||
{
|
||||
if (Orm.CodeFirst.GetTableByEntity(entityType) == null)
|
||||
throw new ArgumentException($"参数 data 类型错误 {entityType.FullName} ");
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
public void Add<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Add(data);
|
||||
public void Add<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
this.Set<TEntity>().Add(data);
|
||||
}
|
||||
public void AddRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AddRange(data);
|
||||
|
||||
/// <summary>
|
||||
@ -118,7 +128,11 @@ namespace FreeSql
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
public void Update<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Update(data);
|
||||
public void Update<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
this.Set<TEntity>().Update(data);
|
||||
}
|
||||
public void UpdateRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().UpdateRange(data);
|
||||
|
||||
/// <summary>
|
||||
@ -126,7 +140,11 @@ namespace FreeSql
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
public void Remove<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Remove(data);
|
||||
public void Remove<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
this.Set<TEntity>().Remove(data);
|
||||
}
|
||||
public void RemoveRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().RemoveRange(data);
|
||||
|
||||
/// <summary>
|
||||
@ -134,21 +152,32 @@ namespace FreeSql
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
public void AddOrUpdate<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().AddOrUpdate(data);
|
||||
|
||||
public void AddOrUpdate<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
this.Set<TEntity>().AddOrUpdate(data);
|
||||
}
|
||||
/// <summary>
|
||||
/// 保存实体的指定 ManyToMany 导航属性
|
||||
/// </summary>
|
||||
/// <param name="data">实体对象</param>
|
||||
/// <param name="propertyName">属性名</param>
|
||||
public void SaveManyToMany<TEntity>(TEntity data, string propertyName) where TEntity : class => this.Set<TEntity>().SaveManyToMany(data, propertyName);
|
||||
public void SaveManyToMany<TEntity>(TEntity data, string propertyName) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
this.Set<TEntity>().SaveManyToMany(data, propertyName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 附加实体,可用于不查询就更新或删除
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="data"></param>
|
||||
public void Attach<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().Attach(data);
|
||||
public void Attach<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
this.Set<TEntity>().Attach(data);
|
||||
}
|
||||
public void AttachRange<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AttachRange(data);
|
||||
|
||||
/// <summary>
|
||||
@ -158,19 +187,36 @@ namespace FreeSql
|
||||
/// <param name="data"></param>
|
||||
public DbContext AttachOnlyPrimary<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
this.Set<TEntity>().AttachOnlyPrimary(data);
|
||||
return this;
|
||||
}
|
||||
#if net40
|
||||
#else
|
||||
public Task AddAsync<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().AddAsync(data);
|
||||
public Task AddAsync<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
return this.Set<TEntity>().AddAsync(data);
|
||||
}
|
||||
public Task AddRangeAsync<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().AddRangeAsync(data);
|
||||
|
||||
public Task UpdateAsync<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().UpdateAsync(data);
|
||||
public Task UpdateAsync<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
return this.Set<TEntity>().UpdateAsync(data);
|
||||
}
|
||||
public Task UpdateRangeAsync<TEntity>(IEnumerable<TEntity> data) where TEntity : class => this.Set<TEntity>().UpdateRangeAsync(data);
|
||||
|
||||
public Task AddOrUpdateAsync<TEntity>(TEntity data) where TEntity : class => this.Set<TEntity>().AddOrUpdateAsync(data);
|
||||
public Task SaveManyToManyAsync<TEntity>(TEntity data, string propertyName) where TEntity : class => this.Set<TEntity>().SaveManyToManyAsync(data, propertyName);
|
||||
public Task AddOrUpdateAsync<TEntity>(TEntity data) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
return this.Set<TEntity>().AddOrUpdateAsync(data);
|
||||
}
|
||||
public Task SaveManyToManyAsync<TEntity>(TEntity data, string propertyName) where TEntity : class
|
||||
{
|
||||
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||
return this.Set<TEntity>().SaveManyToManyAsync(data, propertyName);
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
|
||||
|
@ -133,8 +133,17 @@ namespace FreeSql
|
||||
{
|
||||
if (_dicDbSetObjects.TryGetValue(et, out var tryds)) return tryds;
|
||||
_dicDbSetObjects.Add(et, tryds = _db.Set<object>().AsType(et));
|
||||
if (_db.InternalDicSet.TryGetValue(et, out var tryds2))
|
||||
{
|
||||
var copyTo = typeof(DbSet<>).MakeGenericType(et).GetMethod("StatesCopyToDbSetObject", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(DbSet<object>) }, null);
|
||||
copyTo?.Invoke(tryds2, new object[] { tryds });
|
||||
}
|
||||
return tryds;
|
||||
}
|
||||
void StatesCopyToDbSetObject(DbSet<object> ds)
|
||||
{
|
||||
ds.AttachRange(_states.Values.OrderBy(a => a.Time).Select(a => a.Value).ToArray());
|
||||
}
|
||||
|
||||
public class EntityState
|
||||
{
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<Version>0.11.15</Version>
|
||||
<Version>0.11.18</Version>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql is the most convenient ORM in .NetCore, .NetFramework, And Xamarin. It supports Mysql, Postgresql, SqlServer, Oracle, Sqlite, And Odbc.</Description>
|
||||
|
@ -106,6 +106,13 @@
|
||||
清空状态数据
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
|
||||
<summary>
|
||||
根据 lambda 条件删除数据
|
||||
</summary>
|
||||
<param name="predicate"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.DbSet`1.Add(`0)">
|
||||
<summary>
|
||||
添加
|
||||
|
Reference in New Issue
Block a user