## v0.5.1(五一版)

- 增加 ISelect/IInsert/IUpdate/IDelete.AsType 实现弱类型curd,如:Select<object>().AsType(实体类型);
- 补充 ISelect.From<T2>;
- 补充 ExpressionTree 单元测试;
- 优化 ToList(a => new Dto()),会按优先级查询 Join 实体属性;
- 补充 IDelete/ISelect/IUpdate WhereDynamic 方法,实现 dywhere 条件;
- 修复 WhereObject 内部方法,当开启 Lazy 延时属性时,并且传递实体查询时条件无效;
This commit is contained in:
28810 2019-04-17 00:52:02 +08:00
parent f011d51f3b
commit bada8ad3cc
12 changed files with 88 additions and 38 deletions

View File

@ -23,8 +23,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <param name="_table"></param> /// <param name="_table"></param>
/// <param name="entity"></param> /// <param name="entity"></param>
/// <returns></returns> /// <returns></returns>
public static string GetEntityKeyString<TEntity>(this IFreeSql orm, TEntity entity, string splitString = "*|_,[,_|*") => //public static string GetEntityKeyString<TEntity>(this IFreeSql orm, TEntity entity, string splitString = "*|_,[,_|*") => GetEntityKeyString(orm, typeof(TEntity), entity, splitString);
GetEntityKeyString(orm, typeof(TEntity), entity, splitString);
public static string GetEntityKeyString(this IFreeSql orm, Type entityType, object entity, string splitString = "*|_,[,_|*") { public static string GetEntityKeyString(this IFreeSql orm, Type entityType, object entity, string splitString = "*|_,[,_|*") {
if (entity == null) return null; if (entity == null) return null;
if (entityType == null) entityType = entity.GetType(); if (entityType == null) entityType = entity.GetType();
@ -110,8 +109,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <param name="_table"></param> /// <param name="_table"></param>
/// <param name="entity"></param> /// <param name="entity"></param>
/// <returns></returns> /// <returns></returns>
public static object[] GetEntityKeyValues<TEntity>(this IFreeSql orm, TEntity entity) => //public static object[] GetEntityKeyValues<TEntity>(this IFreeSql orm, TEntity entity) => GetEntityKeyValues(orm, typeof(TEntity), entity);
GetEntityKeyValues(orm, typeof(TEntity), entity);
public static object[] GetEntityKeyValues(this IFreeSql orm, Type entityType, object entity) { public static object[] GetEntityKeyValues(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return null; if (entity == null) return null;
if (entityType == null) entityType = entity.GetType(); if (entityType == null) entityType = entity.GetType();
@ -150,8 +148,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <param name="_table"></param> /// <param name="_table"></param>
/// <param name="entity"></param> /// <param name="entity"></param>
/// <returns></returns> /// <returns></returns>
public static string GetEntityString<TEntity>(this IFreeSql orm, TEntity entity) => //public static string GetEntityString<TEntity>(this IFreeSql orm, TEntity entity) => GetEntityString(orm, typeof(TEntity), entity);
GetEntityString(orm, typeof(TEntity), entity);
public static string GetEntityString(this IFreeSql orm, Type entityType, object entity) { public static string GetEntityString(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return null; if (entity == null) return null;
if (entityType == null) entityType = entity.GetType(); if (entityType == null) entityType = entity.GetType();
@ -195,8 +192,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// 使用新实体的值,复盖旧实体的值 /// 使用新实体的值,复盖旧实体的值
/// </summary> /// </summary>
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>> _dicMapEntityValue = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>>(); static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>> _dicMapEntityValue = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>>();
public static void MapEntityValue<TEntity>(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) => //public static void MapEntityValue<TEntity>(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) => MapEntityValue(orm, typeof(TEntity), entityFrom, entityTo);
MapEntityValue(orm, typeof(TEntity), entityFrom, entityTo);
public static void MapEntityValue(this IFreeSql orm, Type entityType, object entityFrom, object entityTo) { public static void MapEntityValue(this IFreeSql orm, Type entityType, object entityFrom, object entityTo) {
if (entityType == null) entityType = entityFrom?.GetType() ?? entityTo?.GetType(); if (entityType == null) entityType = entityFrom?.GetType() ?? entityTo?.GetType();
var func = _dicMapEntityValue.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object, object>>()).GetOrAdd(entityType, t => { var func = _dicMapEntityValue.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object, object>>()).GetOrAdd(entityType, t => {
@ -235,8 +231,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <summary> /// <summary>
/// 使用新实体的主键值,复盖旧实体的主键值 /// 使用新实体的主键值,复盖旧实体的主键值
/// </summary> /// </summary>
public static void MapEntityKeyValue<TEntity>(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) => //public static void MapEntityKeyValue<TEntity>(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) => MapEntityKeyValue(orm, typeof(TEntity), entityFrom, entityTo);
MapEntityKeyValue(orm, typeof(TEntity), entityFrom, entityTo);
public static void MapEntityKeyValue(this IFreeSql orm, Type entityType, object entityFrom, object entityTo) { public static void MapEntityKeyValue(this IFreeSql orm, Type entityType, object entityFrom, object entityTo) {
if (entityType == null) entityType = entityFrom?.GetType() ?? entityTo?.GetType(); if (entityType == null) entityType = entityFrom?.GetType() ?? entityTo?.GetType();
var func = _dicMapEntityKeyValue.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object, object>>()).GetOrAdd(entityType, t => { var func = _dicMapEntityKeyValue.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Action<object, object>>()).GetOrAdd(entityType, t => {
@ -271,8 +266,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <param name="orm"></param> /// <param name="orm"></param>
/// <param name="entity"></param> /// <param name="entity"></param>
/// <param name="idtval"></param> /// <param name="idtval"></param>
public static void SetEntityIdentityValueWithPrimary<TEntity>(this IFreeSql orm, TEntity entity, long idtval) => //public static void SetEntityIdentityValueWithPrimary<TEntity>(this IFreeSql orm, TEntity entity, long idtval) => SetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity, idtval);
SetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity, idtval);
public static void SetEntityIdentityValueWithPrimary(this IFreeSql orm, Type entityType, object entity, long idtval) { public static void SetEntityIdentityValueWithPrimary(this IFreeSql orm, Type entityType, object entity, long idtval) {
if (entity == null) return; if (entity == null) return;
if (entityType == null) entityType = entity.GetType(); if (entityType == null) entityType = entity.GetType();
@ -305,8 +299,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <typeparam name="TEntity"></typeparam> /// <typeparam name="TEntity"></typeparam>
/// <param name="orm"></param> /// <param name="orm"></param>
/// <param name="entity"></param> /// <param name="entity"></param>
public static long GetEntityIdentityValueWithPrimary<TEntity>(this IFreeSql orm, TEntity entity) => //public static long GetEntityIdentityValueWithPrimary<TEntity>(this IFreeSql orm, TEntity entity) => GetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity);
GetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity);
public static long GetEntityIdentityValueWithPrimary(this IFreeSql orm, Type entityType, object entity) { public static long GetEntityIdentityValueWithPrimary(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return 0; if (entity == null) return 0;
if (entityType == null) entityType = entity.GetType(); if (entityType == null) entityType = entity.GetType();
@ -353,8 +346,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <typeparam name="TEntity"></typeparam> /// <typeparam name="TEntity"></typeparam>
/// <param name="orm"></param> /// <param name="orm"></param>
/// <param name="entity"></param> /// <param name="entity"></param>
public static void ClearEntityPrimaryValueWithIdentityAndGuid<TEntity>(this IFreeSql orm, TEntity entity) => //public static void ClearEntityPrimaryValueWithIdentityAndGuid<TEntity>(this IFreeSql orm, TEntity entity) => ClearEntityPrimaryValueWithIdentityAndGuid(orm, typeof(TEntity), entity);
ClearEntityPrimaryValueWithIdentityAndGuid(orm, typeof(TEntity), entity);
public static void ClearEntityPrimaryValueWithIdentityAndGuid(this IFreeSql orm, Type entityType, object entity) { public static void ClearEntityPrimaryValueWithIdentityAndGuid(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return; if (entity == null) return;
if (entityType == null) entityType = entity.GetType(); if (entityType == null) entityType = entity.GetType();
@ -389,8 +381,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <typeparam name="TEntity"></typeparam> /// <typeparam name="TEntity"></typeparam>
/// <param name="orm"></param> /// <param name="orm"></param>
/// <param name="entity"></param> /// <param name="entity"></param>
public static void ClearEntityPrimaryValueWithIdentity<TEntity>(this IFreeSql orm, TEntity entity) => //public static void ClearEntityPrimaryValueWithIdentity<TEntity>(this IFreeSql orm, TEntity entity) => ClearEntityPrimaryValueWithIdentity(orm, typeof(TEntity), entity);
ClearEntityPrimaryValueWithIdentity(orm, typeof(TEntity), entity);
public static void ClearEntityPrimaryValueWithIdentity(this IFreeSql orm, Type entityType, object entity) { public static void ClearEntityPrimaryValueWithIdentity(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return; if (entity == null) return;
if (entityType == null) entityType = entity.GetType(); if (entityType == null) entityType = entity.GetType();
@ -426,8 +417,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <param name="entity1"></param> /// <param name="entity1"></param>
/// <param name="entity2"></param> /// <param name="entity2"></param>
/// <returns></returns> /// <returns></returns>
public static string[] CompareEntityValueReturnColumns<TEntity>(this IFreeSql orm, TEntity entity1, TEntity entity2, bool isEqual) => //public static string[] CompareEntityValueReturnColumns<TEntity>(this IFreeSql orm, TEntity entity1, TEntity entity2, bool isEqual) => CompareEntityValueReturnColumns(orm, typeof(TEntity), entity1, entity2, isEqual);
CompareEntityValueReturnColumns(orm, typeof(TEntity), entity1, entity2, isEqual);
public static string[] CompareEntityValueReturnColumns(this IFreeSql orm, Type entityType, object entity1, object entity2, bool isEqual) { public static string[] CompareEntityValueReturnColumns(this IFreeSql orm, Type entityType, object entity1, object entity2, bool isEqual) {
if (entityType == null) entityType = entity1?.GetType() ?? entity2?.GetType(); if (entityType == null) entityType = entity1?.GetType() ?? entity2?.GetType();
var func = _dicCompareEntityValueReturnColumns.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, object, bool, string[]>>()).GetOrAdd(entityType, t => { var func = _dicCompareEntityValueReturnColumns.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary<Type, Func<object, object, bool, string[]>>()).GetOrAdd(entityType, t => {
@ -481,8 +471,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <param name="entity"></param> /// <param name="entity"></param>
/// <param name="propertyName"></param> /// <param name="propertyName"></param>
/// <param name="value"></param> /// <param name="value"></param>
public static void SetEntityIncrByWithPropertyName<TEntity>(this IFreeSql orm, TEntity entity, string propertyName, int incrBy) => //public static void SetEntityIncrByWithPropertyName<TEntity>(this IFreeSql orm, TEntity entity, string propertyName, int incrBy) => SetEntityIncrByWithPropertyName(orm, typeof(TEntity), entity, propertyName, incrBy);
SetEntityIncrByWithPropertyName(orm, typeof(TEntity), entity, propertyName, incrBy);
public static void SetEntityIncrByWithPropertyName(this IFreeSql orm, Type entityType, object entity, string propertyName, int incrBy) { public static void SetEntityIncrByWithPropertyName(this IFreeSql orm, Type entityType, object entity, string propertyName, int incrBy) {
if (entity == null) return; if (entity == null) return;
if (entityType == null) entityType = entity.GetType(); if (entityType == null) entityType = entity.GetType();
@ -524,8 +513,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// <param name="entity"></param> /// <param name="entity"></param>
/// <param name="propertyName"></param> /// <param name="propertyName"></param>
/// <param name="value"></param> /// <param name="value"></param>
public static void SetEntityValueWithPropertyName<TEntity>(this IFreeSql orm, TEntity entity, string propertyName, object value) => //public static void SetEntityValueWithPropertyName<TEntity>(this IFreeSql orm, TEntity entity, string propertyName, object value) => SetEntityValueWithPropertyName(orm, typeof(TEntity), entity, propertyName, value);
SetEntityValueWithPropertyName(orm, typeof(TEntity), entity, propertyName, value);
public static void SetEntityValueWithPropertyName(this IFreeSql orm, Type entityType, object entity, string propertyName, object value) { public static void SetEntityValueWithPropertyName(this IFreeSql orm, Type entityType, object entity, string propertyName, object value) {
if (entity == null) return; if (entity == null) return;
if (entityType == null) entityType = entity.GetType(); if (entityType == null) entityType = entity.GetType();
@ -545,15 +533,12 @@ namespace FreeSql.Extensions.EntityUtil {
var prop = _table.Properties[pn]; var prop = _table.Properties[pn];
exps.Add( exps.Add(
Expression.Assign( Expression.Assign(
Expression.MakeMemberAccess(var1Parm, prop),
Expression.Add(
Expression.MakeMemberAccess(var1Parm, prop), Expression.MakeMemberAccess(var1Parm, prop),
Expression.Convert( Expression.Convert(
FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(prop.PropertyType, parm3), FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(prop.PropertyType, parm3),
prop.PropertyType prop.PropertyType
) )
) )
)
); );
} }
return Expression.Lambda<Action<object, string, object>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1, parm2, parm3 }).Compile(); return Expression.Lambda<Action<object, string, object>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1, parm2, parm3 }).Compile();

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<Version>0.4.16.1</Version> <Version>0.5.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>

View File

@ -53,6 +53,12 @@ namespace FreeSql {
/// <param name="notExists">不存在</param> /// <param name="notExists">不存在</param>
/// <returns></returns> /// <returns></returns>
IDelete<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class; IDelete<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class;
/// <summary>
/// 传入动态对象如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
/// </summary>
/// <param name="dywhere">主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合</param>
/// <returns></returns>
IDelete<T1> WhereDynamic(object dywhere);
/// <summary> /// <summary>
/// 设置表名规则,可用于分库/分表参数1默认表名返回值新表名 /// 设置表名规则,可用于分库/分表参数1默认表名返回值新表名
@ -61,6 +67,12 @@ namespace FreeSql {
/// <returns></returns> /// <returns></returns>
IDelete<T1> AsTable(Func<string, string> tableRule); IDelete<T1> AsTable(Func<string, string> tableRule);
/// <summary> /// <summary>
/// 动态Type在使用 Delete&lt;object&gt; 后使用本方法,指定实体类型
/// </summary>
/// <param name="entityType"></param>
/// <returns></returns>
IDelete<T1> AsType(Type entityType);
/// <summary>
/// 返回即将执行的SQL语句 /// 返回即将执行的SQL语句
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>

View File

@ -65,6 +65,12 @@ namespace FreeSql {
/// <returns></returns> /// <returns></returns>
IInsert<T1> AsTable(Func<string, string> tableRule); IInsert<T1> AsTable(Func<string, string> tableRule);
/// <summary> /// <summary>
/// 动态Type在使用 Insert&lt;object&gt; 后使用本方法,指定实体类型
/// </summary>
/// <param name="entityType"></param>
/// <returns></returns>
IInsert<T1> AsType(Type entityType);
/// <summary>
/// 返回即将执行的SQL语句 /// 返回即将执行的SQL语句
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>

View File

@ -259,6 +259,12 @@ namespace FreeSql {
/// <param name="exp">lambda表达式</param> /// <param name="exp">lambda表达式</param>
/// <returns></returns> /// <returns></returns>
ISelect<T1> Where<T2, T3, T4, T5>(Expression<Func<T1, T2, T3, T4, T5, bool>> exp) where T2 : class where T3 : class where T4 : class where T5 : class; ISelect<T1> Where<T2, T3, T4, T5>(Expression<Func<T1, T2, T3, T4, T5, bool>> exp) where T2 : class where T3 : class where T4 : class where T5 : class;
/// <summary>
/// 传入动态对象如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
/// </summary>
/// <param name="dywhere">主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合</param>
/// <returns></returns>
ISelect<T1> WhereDynamic(object dywhere);
/// <summary> /// <summary>
/// 按选择的列分组GroupBy(a => a.Name) | GroupBy(a => new{a.Name,a.Time}) /// 按选择的列分组GroupBy(a => a.Name) | GroupBy(a => new{a.Name,a.Time})

View File

@ -120,6 +120,12 @@ namespace FreeSql {
/// <param name="notExists">不存在</param> /// <param name="notExists">不存在</param>
/// <returns></returns> /// <returns></returns>
IUpdate<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class; IUpdate<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class;
/// <summary>
/// 传入动态对象如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
/// </summary>
/// <param name="dywhere">主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合</param>
/// <returns></returns>
IUpdate<T1> WhereDynamic(object dywhere);
/// <summary> /// <summary>
/// 设置表名规则,可用于分库/分表参数1默认表名返回值新表名 /// 设置表名规则,可用于分库/分表参数1默认表名返回值新表名
@ -128,6 +134,12 @@ namespace FreeSql {
/// <returns></returns> /// <returns></returns>
IUpdate<T1> AsTable(Func<string, string> tableRule); IUpdate<T1> AsTable(Func<string, string> tableRule);
/// <summary> /// <summary>
/// 动态Type在使用 Update&lt;object&gt; 后使用本方法,指定实体类型
/// </summary>
/// <param name="entityType"></param>
/// <returns></returns>
IUpdate<T1> AsType(Type entityType);
/// <summary>
/// 返回即将执行的SQL语句 /// 返回即将执行的SQL语句
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>

View File

@ -28,7 +28,7 @@ namespace FreeSql.Internal.CommonProvider {
_commonExpression = commonExpression; _commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1)); _table = _commonUtils.GetTableByEntity(typeof(T1));
this.Where(_commonUtils.WhereObject(_table, "", dywhere)); this.Where(_commonUtils.WhereObject(_table, "", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure<T1>(); if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
} }
protected void ClearData() { protected void ClearData() {
@ -81,11 +81,21 @@ namespace FreeSql.Internal.CommonProvider {
public IDelete<T1> Where(T1 item) => this.Where(new[] { item }); public IDelete<T1> Where(T1 item) => this.Where(new[] { item });
public IDelete<T1> Where(IEnumerable<T1> items) => this.Where(_commonUtils.WhereItems(_table, "", items)); public IDelete<T1> Where(IEnumerable<T1> items) => this.Where(_commonUtils.WhereItems(_table, "", items));
public IDelete<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class => this.Where($"{(notExists ? "NOT " : "")}EXISTS({select.ToSql("1")})"); public IDelete<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class => this.Where($"{(notExists ? "NOT " : "")}EXISTS({select.ToSql("1")})");
public IDelete<T1> WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_table, "", dywhere));
public IDelete<T1> AsTable(Func<string, string> tableRule) { public IDelete<T1> AsTable(Func<string, string> tableRule) {
_tableRule = tableRule; _tableRule = tableRule;
return this; return this;
} }
public IDelete<T1> AsType(Type entityType) {
if (entityType == typeof(object)) throw new Exception("IDelete.AsType 参数不支持指定为 object");
if (entityType == _table.Type) return this;
var newtb = _commonUtils.GetTableByEntity(entityType);
_table = newtb ?? throw new Exception("IDelete.AsType 参数错误,请传入正确的实体类型");
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this;
}
public string ToSql() => _whereTimes <= 0 ? null : new StringBuilder().Append("DELETE FROM ").Append(_commonUtils.QuoteSqlName(_tableRule?.Invoke(_table.DbName) ?? _table.DbName)).Append(" WHERE ").Append(_where).ToString(); public string ToSql() => _whereTimes <= 0 ? null : new StringBuilder().Append("DELETE FROM ").Append(_commonUtils.QuoteSqlName(_tableRule?.Invoke(_table.DbName) ?? _table.DbName)).Append(" WHERE ").Append(_where).ToString();
} }
} }

View File

@ -30,7 +30,7 @@ namespace FreeSql.Internal.CommonProvider {
_commonExpression = commonExpression; _commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1)); _table = _commonUtils.GetTableByEntity(typeof(T1));
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter; _noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure<T1>(); if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
} }
protected void ClearData() { protected void ClearData() {
@ -356,6 +356,15 @@ namespace FreeSql.Internal.CommonProvider {
_tableRule = tableRule; _tableRule = tableRule;
return this; return this;
} }
public IInsert<T1> AsType(Type entityType) {
if (entityType == typeof(object)) throw new Exception("IInsert.AsType 参数不支持指定为 object");
if (entityType == _table.Type) return this;
var newtb = _commonUtils.GetTableByEntity(entityType);
_table = newtb ?? throw new Exception("IInsert.AsType 参数错误,请传入正确的实体类型");
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this;
}
public virtual string ToSql() { public virtual string ToSql() {
if (_source == null || _source.Any() == false) return null; if (_source == null || _source.Any() == false) return null;
var sb = new StringBuilder(); var sb = new StringBuilder();

View File

@ -523,8 +523,7 @@ namespace FreeSql.Internal.CommonProvider {
if (entityType == typeof(object)) throw new Exception("ISelect.AsType 参数不支持指定为 object"); if (entityType == typeof(object)) throw new Exception("ISelect.AsType 参数不支持指定为 object");
if (entityType == _tables[0].Table.Type) return this as TSelect; if (entityType == _tables[0].Table.Type) return this as TSelect;
var newtb = _commonUtils.GetTableByEntity(entityType); var newtb = _commonUtils.GetTableByEntity(entityType);
if (newtb == null) throw new Exception("ISelect.AsType 参数错误,请传入正确的实体类型"); _tables[0].Table = newtb ?? throw new Exception("ISelect.AsType 参数错误,请传入正确的实体类型");
_tables[0].Table = newtb;
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType); if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this as TSelect; return this as TSelect;
} }

View File

@ -218,6 +218,7 @@ namespace FreeSql.Internal.CommonProvider {
_tables[0].Parameter = exp.Parameters[0]; _tables[0].Parameter = exp.Parameters[0];
return this.InternalWhere(exp?.Body); return this.InternalWhere(exp?.Body);
} }
public ISelect<T1> WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_tables.First().Table, $"{_tables.First().Alias}.", dywhere));
public ISelect<T1> WhereIf(bool condition, Expression<Func<T1, bool>> exp) { public ISelect<T1> WhereIf(bool condition, Expression<Func<T1, bool>> exp) {
if (condition == false) return this; if (condition == false) return this;

View File

@ -35,7 +35,7 @@ namespace FreeSql.Internal.CommonProvider {
_table = _commonUtils.GetTableByEntity(typeof(T1)); _table = _commonUtils.GetTableByEntity(typeof(T1));
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter; _noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
this.Where(_commonUtils.WhereObject(_table, "", dywhere)); this.Where(_commonUtils.WhereObject(_table, "", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure<T1>(); if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
} }
protected void ClearData() { protected void ClearData() {
@ -69,7 +69,7 @@ namespace FreeSql.Internal.CommonProvider {
if (affrows != _source.Count) if (affrows != _source.Count)
throw new Exception($"记录可能不存在,或者【行级乐观锁】版本过旧,更新数量{_source.Count},影响的行数{affrows}。"); throw new Exception($"记录可能不存在,或者【行级乐观锁】版本过旧,更新数量{_source.Count},影响的行数{affrows}。");
foreach (var d in _source) foreach (var d in _source)
_orm.SetEntityIncrByWithPropertyName(d, _table.VersionColumn.CsName, 1); _orm.SetEntityIncrByWithPropertyName(_table.Type, d, _table.VersionColumn.CsName, 1);
} }
} }
@ -347,6 +347,7 @@ namespace FreeSql.Internal.CommonProvider {
public IUpdate<T1> Where(T1 item) => this.Where(new[] { item }); public IUpdate<T1> Where(T1 item) => this.Where(new[] { item });
public IUpdate<T1> Where(IEnumerable<T1> items) => this.Where(_commonUtils.WhereItems(_table, "", items)); public IUpdate<T1> Where(IEnumerable<T1> items) => this.Where(_commonUtils.WhereItems(_table, "", items));
public IUpdate<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class => this.Where($"{(notExists ? "NOT " : "")}EXISTS({select.ToSql("1")})"); public IUpdate<T1> WhereExists<TEntity2>(ISelect<TEntity2> select, bool notExists = false) where TEntity2 : class => this.Where($"{(notExists ? "NOT " : "")}EXISTS({select.ToSql("1")})");
public IUpdate<T1> WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_table, "", dywhere));
protected string WhereCaseSource(string CsName, Func<string, string> thenValue) { protected string WhereCaseSource(string CsName, Func<string, string> thenValue) {
if (_source.Any() == false) return null; if (_source.Any() == false) return null;
@ -401,6 +402,15 @@ namespace FreeSql.Internal.CommonProvider {
_tableRule = tableRule; _tableRule = tableRule;
return this; return this;
} }
public IUpdate<T1> AsType(Type entityType) {
if (entityType == typeof(object)) throw new Exception("IUpdate.AsType 参数不支持指定为 object");
if (entityType == _table.Type) return this;
var newtb = _commonUtils.GetTableByEntity(entityType);
_table = newtb ?? throw new Exception("IUpdate.AsType 参数错误,请传入正确的实体类型");
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this;
}
public string ToSql() { public string ToSql() {
if (_where.Length == 0 && _source.Any() == false) return null; if (_where.Length == 0 && _source.Any() == false) return null;

View File

@ -91,7 +91,7 @@ namespace FreeSql.Internal {
var primarys = table.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray(); var primarys = table.Columns.Values.Where(a => a.Attribute.IsPrimary == true).ToArray();
if (primarys.Length == 1 && (type == primarys.First().CsType || type.IsNumberType() && primarys.First().CsType.IsNumberType())) { if (primarys.Length == 1 && (type == primarys.First().CsType || type.IsNumberType() && primarys.First().CsType.IsNumberType())) {
return $"{aliasAndDot}{this.QuoteSqlName(primarys.First().Attribute.Name)} = {this.FormatSql("{0}", dywhere)}"; return $"{aliasAndDot}{this.QuoteSqlName(primarys.First().Attribute.Name)} = {this.FormatSql("{0}", dywhere)}";
} else if (primarys.Length > 0 && type.FullName == table.Type.FullName) { } else if (primarys.Length > 0 && (type == table.Type || type.BaseType == table.Type)) {
var sb = new StringBuilder(); var sb = new StringBuilder();
var pkidx = 0; var pkidx = 0;
foreach (var pk in primarys) { foreach (var pk in primarys) {