diff --git a/FreeSql/Extensions/EntityUtilExtensions.cs b/FreeSql/Extensions/EntityUtilExtensions.cs
index 19e34f0f..5824cea6 100644
--- a/FreeSql/Extensions/EntityUtilExtensions.cs
+++ b/FreeSql/Extensions/EntityUtilExtensions.cs
@@ -23,8 +23,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static string GetEntityKeyString(this IFreeSql orm, TEntity entity, string splitString = "*|_,[,_|*") =>
- GetEntityKeyString(orm, typeof(TEntity), entity, splitString);
+ //public static string GetEntityKeyString(this IFreeSql orm, TEntity entity, string splitString = "*|_,[,_|*") => GetEntityKeyString(orm, typeof(TEntity), entity, splitString);
public static string GetEntityKeyString(this IFreeSql orm, Type entityType, object entity, string splitString = "*|_,[,_|*") {
if (entity == null) return null;
if (entityType == null) entityType = entity.GetType();
@@ -110,8 +109,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static object[] GetEntityKeyValues(this IFreeSql orm, TEntity entity) =>
- GetEntityKeyValues(orm, typeof(TEntity), entity);
+ //public static object[] GetEntityKeyValues(this IFreeSql orm, TEntity entity) => GetEntityKeyValues(orm, typeof(TEntity), entity);
public static object[] GetEntityKeyValues(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return null;
if (entityType == null) entityType = entity.GetType();
@@ -150,8 +148,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static string GetEntityString(this IFreeSql orm, TEntity entity) =>
- GetEntityString(orm, typeof(TEntity), entity);
+ //public static string GetEntityString(this IFreeSql orm, TEntity entity) => GetEntityString(orm, typeof(TEntity), entity);
public static string GetEntityString(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return null;
if (entityType == null) entityType = entity.GetType();
@@ -195,8 +192,7 @@ namespace FreeSql.Extensions.EntityUtil {
/// 使用新实体的值,复盖旧实体的值
///
static ConcurrentDictionary>> _dicMapEntityValue = new ConcurrentDictionary>>();
- public static void MapEntityValue(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) =>
- MapEntityValue(orm, typeof(TEntity), entityFrom, entityTo);
+ //public static void MapEntityValue(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) => MapEntityValue(orm, typeof(TEntity), entityFrom, entityTo);
public static void MapEntityValue(this IFreeSql orm, Type entityType, object entityFrom, object entityTo) {
if (entityType == null) entityType = entityFrom?.GetType() ?? entityTo?.GetType();
var func = _dicMapEntityValue.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary>()).GetOrAdd(entityType, t => {
@@ -235,8 +231,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
/// 使用新实体的主键值,复盖旧实体的主键值
///
- public static void MapEntityKeyValue(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) =>
- MapEntityKeyValue(orm, typeof(TEntity), entityFrom, entityTo);
+ //public static void MapEntityKeyValue(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) => MapEntityKeyValue(orm, typeof(TEntity), entityFrom, entityTo);
public static void MapEntityKeyValue(this IFreeSql orm, Type entityType, object entityFrom, object entityTo) {
if (entityType == null) entityType = entityFrom?.GetType() ?? entityTo?.GetType();
var func = _dicMapEntityKeyValue.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary>()).GetOrAdd(entityType, t => {
@@ -271,8 +266,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static void SetEntityIdentityValueWithPrimary(this IFreeSql orm, TEntity entity, long idtval) =>
- SetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity, idtval);
+ //public static void SetEntityIdentityValueWithPrimary(this IFreeSql orm, TEntity entity, long idtval) => SetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity, idtval);
public static void SetEntityIdentityValueWithPrimary(this IFreeSql orm, Type entityType, object entity, long idtval) {
if (entity == null) return;
if (entityType == null) entityType = entity.GetType();
@@ -305,8 +299,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static long GetEntityIdentityValueWithPrimary(this IFreeSql orm, TEntity entity) =>
- GetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity);
+ //public static long GetEntityIdentityValueWithPrimary(this IFreeSql orm, TEntity entity) => GetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity);
public static long GetEntityIdentityValueWithPrimary(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return 0;
if (entityType == null) entityType = entity.GetType();
@@ -353,8 +346,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static void ClearEntityPrimaryValueWithIdentityAndGuid(this IFreeSql orm, TEntity entity) =>
- ClearEntityPrimaryValueWithIdentityAndGuid(orm, typeof(TEntity), entity);
+ //public static void ClearEntityPrimaryValueWithIdentityAndGuid(this IFreeSql orm, TEntity entity) => ClearEntityPrimaryValueWithIdentityAndGuid(orm, typeof(TEntity), entity);
public static void ClearEntityPrimaryValueWithIdentityAndGuid(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return;
if (entityType == null) entityType = entity.GetType();
@@ -389,8 +381,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static void ClearEntityPrimaryValueWithIdentity(this IFreeSql orm, TEntity entity) =>
- ClearEntityPrimaryValueWithIdentity(orm, typeof(TEntity), entity);
+ //public static void ClearEntityPrimaryValueWithIdentity(this IFreeSql orm, TEntity entity) => ClearEntityPrimaryValueWithIdentity(orm, typeof(TEntity), entity);
public static void ClearEntityPrimaryValueWithIdentity(this IFreeSql orm, Type entityType, object entity) {
if (entity == null) return;
if (entityType == null) entityType = entity.GetType();
@@ -426,8 +417,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static string[] CompareEntityValueReturnColumns(this IFreeSql orm, TEntity entity1, TEntity entity2, bool isEqual) =>
- CompareEntityValueReturnColumns(orm, typeof(TEntity), entity1, entity2, isEqual);
+ //public static string[] CompareEntityValueReturnColumns(this IFreeSql orm, TEntity entity1, TEntity entity2, bool isEqual) => CompareEntityValueReturnColumns(orm, typeof(TEntity), entity1, entity2, isEqual);
public static string[] CompareEntityValueReturnColumns(this IFreeSql orm, Type entityType, object entity1, object entity2, bool isEqual) {
if (entityType == null) entityType = entity1?.GetType() ?? entity2?.GetType();
var func = _dicCompareEntityValueReturnColumns.GetOrAdd(orm.Ado.DataType, dt => new ConcurrentDictionary>()).GetOrAdd(entityType, t => {
@@ -481,8 +471,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static void SetEntityIncrByWithPropertyName(this IFreeSql orm, TEntity entity, string propertyName, int incrBy) =>
- SetEntityIncrByWithPropertyName(orm, typeof(TEntity), entity, propertyName, incrBy);
+ //public static void SetEntityIncrByWithPropertyName(this IFreeSql orm, TEntity entity, string propertyName, int incrBy) => SetEntityIncrByWithPropertyName(orm, typeof(TEntity), entity, propertyName, incrBy);
public static void SetEntityIncrByWithPropertyName(this IFreeSql orm, Type entityType, object entity, string propertyName, int incrBy) {
if (entity == null) return;
if (entityType == null) entityType = entity.GetType();
@@ -524,8 +513,7 @@ namespace FreeSql.Extensions.EntityUtil {
///
///
///
- public static void SetEntityValueWithPropertyName(this IFreeSql orm, TEntity entity, string propertyName, object value) =>
- SetEntityValueWithPropertyName(orm, typeof(TEntity), entity, propertyName, value);
+ //public static void SetEntityValueWithPropertyName(this IFreeSql orm, TEntity entity, string propertyName, object value) => SetEntityValueWithPropertyName(orm, typeof(TEntity), entity, propertyName, value);
public static void SetEntityValueWithPropertyName(this IFreeSql orm, Type entityType, object entity, string propertyName, object value) {
if (entity == null) return;
if (entityType == null) entityType = entity.GetType();
@@ -546,12 +534,9 @@ namespace FreeSql.Extensions.EntityUtil {
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(var1Parm, prop),
- Expression.Add(
- Expression.MakeMemberAccess(var1Parm, prop),
- Expression.Convert(
- FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(prop.PropertyType, parm3),
- prop.PropertyType
- )
+ Expression.Convert(
+ FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(prop.PropertyType, parm3),
+ prop.PropertyType
)
)
);
diff --git a/FreeSql/FreeSql.csproj b/FreeSql/FreeSql.csproj
index 576b182e..eee11a9c 100644
--- a/FreeSql/FreeSql.csproj
+++ b/FreeSql/FreeSql.csproj
@@ -2,7 +2,7 @@
netstandard2.0
- 0.4.16.1
+ 0.5.1
true
YeXiangQin
FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.
diff --git a/FreeSql/Interface/Curd/IDelete.cs b/FreeSql/Interface/Curd/IDelete.cs
index 5cf4eb20..9b4d7cce 100644
--- a/FreeSql/Interface/Curd/IDelete.cs
+++ b/FreeSql/Interface/Curd/IDelete.cs
@@ -53,6 +53,12 @@ namespace FreeSql {
/// 不存在
///
IDelete WhereExists(ISelect select, bool notExists = false) where TEntity2 : class;
+ ///
+ /// 传入动态对象如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
+ ///
+ /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合
+ ///
+ IDelete WhereDynamic(object dywhere);
///
/// 设置表名规则,可用于分库/分表,参数1:默认表名;返回值:新表名;
@@ -61,6 +67,12 @@ namespace FreeSql {
///
IDelete AsTable(Func tableRule);
///
+ /// 动态Type,在使用 Delete<object> 后使用本方法,指定实体类型
+ ///
+ ///
+ ///
+ IDelete AsType(Type entityType);
+ ///
/// 返回即将执行的SQL语句
///
///
diff --git a/FreeSql/Interface/Curd/IInsert.cs b/FreeSql/Interface/Curd/IInsert.cs
index 573fb26d..93b2a3ab 100644
--- a/FreeSql/Interface/Curd/IInsert.cs
+++ b/FreeSql/Interface/Curd/IInsert.cs
@@ -65,6 +65,12 @@ namespace FreeSql {
///
IInsert AsTable(Func tableRule);
///
+ /// 动态Type,在使用 Insert<object> 后使用本方法,指定实体类型
+ ///
+ ///
+ ///
+ IInsert AsType(Type entityType);
+ ///
/// 返回即将执行的SQL语句
///
///
diff --git a/FreeSql/Interface/Curd/ISelect/ISelect1.cs b/FreeSql/Interface/Curd/ISelect/ISelect1.cs
index 2b448b5d..afa80216 100644
--- a/FreeSql/Interface/Curd/ISelect/ISelect1.cs
+++ b/FreeSql/Interface/Curd/ISelect/ISelect1.cs
@@ -259,6 +259,12 @@ namespace FreeSql {
/// lambda表达式
///
ISelect Where(Expression> exp) where T2 : class where T3 : class where T4 : class where T5 : class;
+ ///
+ /// 传入动态对象如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
+ ///
+ /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合
+ ///
+ ISelect WhereDynamic(object dywhere);
///
/// 按选择的列分组,GroupBy(a => a.Name) | GroupBy(a => new{a.Name,a.Time})
diff --git a/FreeSql/Interface/Curd/IUpdate.cs b/FreeSql/Interface/Curd/IUpdate.cs
index ced2f4f5..e0f160ef 100644
--- a/FreeSql/Interface/Curd/IUpdate.cs
+++ b/FreeSql/Interface/Curd/IUpdate.cs
@@ -120,6 +120,12 @@ namespace FreeSql {
/// 不存在
///
IUpdate WhereExists(ISelect select, bool notExists = false) where TEntity2 : class;
+ ///
+ /// 传入动态对象如:主键值 | new[]{主键值1,主键值2} | TEntity1 | new[]{TEntity1,TEntity2} | new{id=1}
+ ///
+ /// 主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合
+ ///
+ IUpdate WhereDynamic(object dywhere);
///
/// 设置表名规则,可用于分库/分表,参数1:默认表名;返回值:新表名;
@@ -128,6 +134,12 @@ namespace FreeSql {
///
IUpdate AsTable(Func tableRule);
///
+ /// 动态Type,在使用 Update<object> 后使用本方法,指定实体类型
+ ///
+ ///
+ ///
+ IUpdate AsType(Type entityType);
+ ///
/// 返回即将执行的SQL语句
///
///
diff --git a/FreeSql/Internal/CommonProvider/DeleteProvider.cs b/FreeSql/Internal/CommonProvider/DeleteProvider.cs
index a23d8478..f410aa6a 100644
--- a/FreeSql/Internal/CommonProvider/DeleteProvider.cs
+++ b/FreeSql/Internal/CommonProvider/DeleteProvider.cs
@@ -28,7 +28,7 @@ namespace FreeSql.Internal.CommonProvider {
_commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1));
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
- if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure();
+ if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure();
}
protected void ClearData() {
@@ -81,11 +81,21 @@ namespace FreeSql.Internal.CommonProvider {
public IDelete Where(T1 item) => this.Where(new[] { item });
public IDelete Where(IEnumerable items) => this.Where(_commonUtils.WhereItems(_table, "", items));
public IDelete WhereExists(ISelect select, bool notExists = false) where TEntity2 : class => this.Where($"{(notExists ? "NOT " : "")}EXISTS({select.ToSql("1")})");
+ public IDelete WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_table, "", dywhere));
public IDelete AsTable(Func tableRule) {
_tableRule = tableRule;
return this;
}
+ public IDelete 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();
}
}
diff --git a/FreeSql/Internal/CommonProvider/InsertProvider.cs b/FreeSql/Internal/CommonProvider/InsertProvider.cs
index c0995198..9e9e3e01 100644
--- a/FreeSql/Internal/CommonProvider/InsertProvider.cs
+++ b/FreeSql/Internal/CommonProvider/InsertProvider.cs
@@ -30,7 +30,7 @@ namespace FreeSql.Internal.CommonProvider {
_commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1));
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
- if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure();
+ if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure();
}
protected void ClearData() {
@@ -356,6 +356,15 @@ namespace FreeSql.Internal.CommonProvider {
_tableRule = tableRule;
return this;
}
+ public IInsert 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() {
if (_source == null || _source.Any() == false) return null;
var sb = new StringBuilder();
diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs
index 1788448c..9e8f2241 100644
--- a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs
+++ b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs
@@ -523,8 +523,7 @@ namespace FreeSql.Internal.CommonProvider {
if (entityType == typeof(object)) throw new Exception("ISelect.AsType 参数不支持指定为 object");
if (entityType == _tables[0].Table.Type) return this as TSelect;
var newtb = _commonUtils.GetTableByEntity(entityType);
- if (newtb == null) throw new Exception("ISelect.AsType 参数错误,请传入正确的实体类型");
- _tables[0].Table = newtb;
+ _tables[0].Table = newtb ?? throw new Exception("ISelect.AsType 参数错误,请传入正确的实体类型");
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this as TSelect;
}
diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs
index eb506d56..632b6bd7 100644
--- a/FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs
+++ b/FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs
@@ -218,6 +218,7 @@ namespace FreeSql.Internal.CommonProvider {
_tables[0].Parameter = exp.Parameters[0];
return this.InternalWhere(exp?.Body);
}
+ public ISelect WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_tables.First().Table, $"{_tables.First().Alias}.", dywhere));
public ISelect WhereIf(bool condition, Expression> exp) {
if (condition == false) return this;
diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs
index 82b0ded1..6f013eab 100644
--- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs
+++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs
@@ -35,7 +35,7 @@ namespace FreeSql.Internal.CommonProvider {
_table = _commonUtils.GetTableByEntity(typeof(T1));
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
- if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure();
+ if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure();
}
protected void ClearData() {
@@ -69,7 +69,7 @@ namespace FreeSql.Internal.CommonProvider {
if (affrows != _source.Count)
throw new Exception($"记录可能不存在,或者【行级乐观锁】版本过旧,更新数量{_source.Count},影响的行数{affrows}。");
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 Where(T1 item) => this.Where(new[] { item });
public IUpdate Where(IEnumerable items) => this.Where(_commonUtils.WhereItems(_table, "", items));
public IUpdate WhereExists(ISelect select, bool notExists = false) where TEntity2 : class => this.Where($"{(notExists ? "NOT " : "")}EXISTS({select.ToSql("1")})");
+ public IUpdate WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_table, "", dywhere));
protected string WhereCaseSource(string CsName, Func thenValue) {
if (_source.Any() == false) return null;
@@ -401,6 +402,15 @@ namespace FreeSql.Internal.CommonProvider {
_tableRule = tableRule;
return this;
}
+ public IUpdate 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() {
if (_where.Length == 0 && _source.Any() == false) return null;
diff --git a/FreeSql/Internal/CommonUtils.cs b/FreeSql/Internal/CommonUtils.cs
index 1294997a..7fccbcd2 100644
--- a/FreeSql/Internal/CommonUtils.cs
+++ b/FreeSql/Internal/CommonUtils.cs
@@ -91,7 +91,7 @@ namespace FreeSql.Internal {
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())) {
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 pkidx = 0;
foreach (var pk in primarys) {