From 5d734052dfcf7707e58ef40dbac29bb9fc9a373e Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Fri, 24 May 2019 18:11:27 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=20=E5=AE=9E=E4=BD=93?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=B1=9E=E6=80=A7=20DbDefaultValue=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=9B=20-=20=E4=BF=AE=E5=A4=8D=20Expressi?= =?UTF-8?q?on=20=E8=A1=A8=E8=BE=BE=E5=BC=8F=E8=A7=A3=E6=9E=90=20Convert=20?= =?UTF-8?q?=E7=9A=84=E5=88=A4=E6=96=AD=20bug=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/restful/Startup.cs | 12 ++++++++++++ FreeSql.Tests/UnitTest1.cs | 2 +- FreeSql/FreeSql.csproj | 4 ++-- FreeSql/Internal/UtilsExpressionTree.cs | 16 +++++++++++----- FreeSql/MySql/MySqlExpression.cs | 4 ++-- FreeSql/Oracle/OracleExpression.cs | 2 +- FreeSql/PostgreSQL/PostgreSQLExpression.cs | 2 +- FreeSql/SqlServer/SqlServerExpression.cs | 2 +- FreeSql/Sqlite/SqliteExpression.cs | 2 +- 9 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Examples/restful/Startup.cs b/Examples/restful/Startup.cs index ac6c5817..3cc16581 100644 --- a/Examples/restful/Startup.cs +++ b/Examples/restful/Startup.cs @@ -17,6 +17,18 @@ namespace restful { .UseLogger(loggerFactory.CreateLogger()) .UseAutoSyncStructure(true) .Build(); + + Fsql.Aop.CurdAfter = (s, e) => { + if (e.ElapsedMilliseconds > 200) { + //记录日志 + //发送短信给负责人 + } + }; + + //Fsql.Aop.Where = (s, e) => { + // if (e.Parameters[0]?.ToString() == "1") + // e.IsCancel = true; + //}; } public IConfiguration Configuration { get; } diff --git a/FreeSql.Tests/UnitTest1.cs b/FreeSql.Tests/UnitTest1.cs index 2217f07a..016444be 100644 --- a/FreeSql.Tests/UnitTest1.cs +++ b/FreeSql.Tests/UnitTest1.cs @@ -126,7 +126,7 @@ namespace FreeSql.Tests { [Fact] public void Test1() { - g.sqlite.CodeFirst.SyncStructure(); + Assert.Throws(() => g.sqlite.CodeFirst.SyncStructure()); var TestEnumable = new TestEnumable(); diff --git a/FreeSql/FreeSql.csproj b/FreeSql/FreeSql.csproj index 66288a1a..bbc28275 100644 --- a/FreeSql/FreeSql.csproj +++ b/FreeSql/FreeSql.csproj @@ -2,7 +2,7 @@ netstandard2.0 - 0.5.22 + 0.5.23 true YeXiangQin FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite. @@ -18,7 +18,7 @@ - C:\Users\28810\Desktop\github\FreeSql\FreeSql\FreeSql.xml + FreeSql.xml 3 diff --git a/FreeSql/Internal/UtilsExpressionTree.cs b/FreeSql/Internal/UtilsExpressionTree.cs index 6fa879d1..6b71f42b 100644 --- a/FreeSql/Internal/UtilsExpressionTree.cs +++ b/FreeSql/Internal/UtilsExpressionTree.cs @@ -109,11 +109,6 @@ namespace FreeSql.Internal { if (tmpLt.Contains("BYTE")) tmpLt = tmpLt.Replace("BYTE", " BYTE"); return tmpLt; }); - colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(Activator.CreateInstance(trytb.Type)); - if (colattr.DbDefautValue != null && p.PropertyType != colattr.MapType) colattr.DbDefautValue = Utils.GetDataReaderValue(colattr.MapType, colattr.DbDefautValue); - if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue; - if (colattr.IsNullable == false && colattr.DbDefautValue == null) - colattr.DbDefautValue = Activator.CreateInstance(colattr.MapType.IsNullableType() ? colattr.MapType.GenericTypeArguments.FirstOrDefault() : colattr.MapType); if (colattr.IsIdentity == true && colattr.MapType.IsNumberType() == false) colattr.IsIdentity = false; if (setMethod == null) colattr.IsIgnore = true; @@ -128,6 +123,17 @@ namespace FreeSql.Internal { trytb.ColumnsByCsIgnore.Add(p.Name, col); continue; } + colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(Activator.CreateInstance(trytb.Type)); + if (colattr.DbDefautValue != null && p.PropertyType != colattr.MapType) colattr.DbDefautValue = Utils.GetDataReaderValue(colattr.MapType, colattr.DbDefautValue); + if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue; + if (colattr.IsNullable == false && colattr.DbDefautValue == null) { + var citype = colattr.MapType.IsNullableType() ? colattr.MapType.GenericTypeArguments.FirstOrDefault() : colattr.MapType; + if (citype.IsArray) + colattr.DbDefautValue = Array.CreateInstance(citype, 0); + else + colattr.DbDefautValue = Activator.CreateInstance(citype); + } + trytb.Columns.Add(colattr.Name, col); trytb.ColumnsByCs.Add(p.Name, col); } diff --git a/FreeSql/MySql/MySqlExpression.cs b/FreeSql/MySql/MySqlExpression.cs index a8664080..f6634806 100644 --- a/FreeSql/MySql/MySqlExpression.cs +++ b/FreeSql/MySql/MySqlExpression.cs @@ -18,8 +18,8 @@ namespace FreeSql.MySql { case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); - if (gentype != exp.Type.NullableTypeOrThis()) { - switch (exp.Type.NullableTypeOrThis().ToString()) { + if (gentype != operandExp.Type.NullableTypeOrThis()) { + switch (gentype.ToString()) { case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))"; case "System.Byte": return $"cast({getExp(operandExp)} as unsigned)"; case "System.Char": return $"substr(cast({getExp(operandExp)} as char), 1, 1)"; diff --git a/FreeSql/Oracle/OracleExpression.cs b/FreeSql/Oracle/OracleExpression.cs index 6dde4826..8003f125 100644 --- a/FreeSql/Oracle/OracleExpression.cs +++ b/FreeSql/Oracle/OracleExpression.cs @@ -18,7 +18,7 @@ namespace FreeSql.Oracle { case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); - if (gentype != exp.Type.NullableTypeOrThis()) { + if (gentype != operandExp.Type.NullableTypeOrThis()) { switch (exp.Type.NullableTypeOrThis().ToString()) { //case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))"; case "System.Byte": return $"cast({getExp(operandExp)} as number)"; diff --git a/FreeSql/PostgreSQL/PostgreSQLExpression.cs b/FreeSql/PostgreSQL/PostgreSQLExpression.cs index 4673d767..2fe5abc5 100644 --- a/FreeSql/PostgreSQL/PostgreSQLExpression.cs +++ b/FreeSql/PostgreSQL/PostgreSQLExpression.cs @@ -19,7 +19,7 @@ namespace FreeSql.PostgreSQL { case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); - if (gentype != exp.Type.NullableTypeOrThis()) { + if (gentype != operandExp.Type.NullableTypeOrThis()) { switch (exp.Type.NullableTypeOrThis().ToString()) { case "System.Boolean": return $"(({getExp(operandExp)})::varchar not in ('0','false','f','no'))"; case "System.Byte": return $"({getExp(operandExp)})::int2"; diff --git a/FreeSql/SqlServer/SqlServerExpression.cs b/FreeSql/SqlServer/SqlServerExpression.cs index 83cfe810..0dc8e19b 100644 --- a/FreeSql/SqlServer/SqlServerExpression.cs +++ b/FreeSql/SqlServer/SqlServerExpression.cs @@ -18,7 +18,7 @@ namespace FreeSql.SqlServer { case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); - if (gentype != exp.Type.NullableTypeOrThis()) { + if (gentype != operandExp.Type.NullableTypeOrThis()) { switch (gentype.ToString()) { case "System.Boolean": return $"(cast({getExp(operandExp)} as varchar) not in ('0','false'))"; case "System.Byte": return $"cast({getExp(operandExp)} as tinyint)"; diff --git a/FreeSql/Sqlite/SqliteExpression.cs b/FreeSql/Sqlite/SqliteExpression.cs index 97d2f80c..4af0e9d1 100644 --- a/FreeSql/Sqlite/SqliteExpression.cs +++ b/FreeSql/Sqlite/SqliteExpression.cs @@ -18,7 +18,7 @@ namespace FreeSql.Sqlite { case ExpressionType.Convert: var operandExp = (exp as UnaryExpression)?.Operand; var gentype = exp.Type.NullableTypeOrThis(); - if (gentype != exp.Type.NullableTypeOrThis()) { + if (gentype != operandExp.Type.NullableTypeOrThis()) { switch (exp.Type.NullableTypeOrThis().ToString()) { case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))"; case "System.Byte": return $"cast({getExp(operandExp)} as int2)";