- 优化 实体数据属性 DbDefaultValue 处理;

- 修复 Expression 表达式解析 Convert 的判断 bug;
This commit is contained in:
28810 2019-05-24 18:11:27 +08:00
parent d54b245ba5
commit 5d734052df
9 changed files with 32 additions and 14 deletions

View File

@ -17,6 +17,18 @@ namespace restful {
.UseLogger(loggerFactory.CreateLogger<IFreeSql>()) .UseLogger(loggerFactory.CreateLogger<IFreeSql>())
.UseAutoSyncStructure(true) .UseAutoSyncStructure(true)
.Build(); .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; } public IConfiguration Configuration { get; }

View File

@ -126,7 +126,7 @@ namespace FreeSql.Tests {
[Fact] [Fact]
public void Test1() { public void Test1() {
g.sqlite.CodeFirst.SyncStructure<TestEnumable>(); Assert.Throws<Exception>(() => g.sqlite.CodeFirst.SyncStructure<TestEnumable>());
var TestEnumable = new TestEnumable(); var TestEnumable = new TestEnumable();

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<Version>0.5.22</Version> <Version>0.5.23</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>
@ -18,7 +18,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>C:\Users\28810\Desktop\github\FreeSql\FreeSql\FreeSql.xml</DocumentationFile> <DocumentationFile>FreeSql.xml</DocumentationFile>
<WarningLevel>3</WarningLevel> <WarningLevel>3</WarningLevel>
</PropertyGroup> </PropertyGroup>

View File

@ -109,11 +109,6 @@ namespace FreeSql.Internal {
if (tmpLt.Contains("BYTE")) tmpLt = tmpLt.Replace("BYTE", " BYTE"); if (tmpLt.Contains("BYTE")) tmpLt = tmpLt.Replace("BYTE", " BYTE");
return tmpLt; 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) if (colattr.IsIdentity == true && colattr.MapType.IsNumberType() == false)
colattr.IsIdentity = false; colattr.IsIdentity = false;
if (setMethod == null) colattr.IsIgnore = true; if (setMethod == null) colattr.IsIgnore = true;
@ -128,6 +123,17 @@ namespace FreeSql.Internal {
trytb.ColumnsByCsIgnore.Add(p.Name, col); trytb.ColumnsByCsIgnore.Add(p.Name, col);
continue; 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.Columns.Add(colattr.Name, col);
trytb.ColumnsByCs.Add(p.Name, col); trytb.ColumnsByCs.Add(p.Name, col);
} }

View File

@ -18,8 +18,8 @@ namespace FreeSql.MySql {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis(); var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) { if (gentype != operandExp.Type.NullableTypeOrThis()) {
switch (exp.Type.NullableTypeOrThis().ToString()) { switch (gentype.ToString()) {
case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))"; case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))";
case "System.Byte": return $"cast({getExp(operandExp)} as unsigned)"; case "System.Byte": return $"cast({getExp(operandExp)} as unsigned)";
case "System.Char": return $"substr(cast({getExp(operandExp)} as char), 1, 1)"; case "System.Char": return $"substr(cast({getExp(operandExp)} as char), 1, 1)";

View File

@ -18,7 +18,7 @@ namespace FreeSql.Oracle {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis(); var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) { if (gentype != operandExp.Type.NullableTypeOrThis()) {
switch (exp.Type.NullableTypeOrThis().ToString()) { switch (exp.Type.NullableTypeOrThis().ToString()) {
//case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))"; //case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))";
case "System.Byte": return $"cast({getExp(operandExp)} as number)"; case "System.Byte": return $"cast({getExp(operandExp)} as number)";

View File

@ -19,7 +19,7 @@ namespace FreeSql.PostgreSQL {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis(); var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) { if (gentype != operandExp.Type.NullableTypeOrThis()) {
switch (exp.Type.NullableTypeOrThis().ToString()) { switch (exp.Type.NullableTypeOrThis().ToString()) {
case "System.Boolean": return $"(({getExp(operandExp)})::varchar not in ('0','false','f','no'))"; case "System.Boolean": return $"(({getExp(operandExp)})::varchar not in ('0','false','f','no'))";
case "System.Byte": return $"({getExp(operandExp)})::int2"; case "System.Byte": return $"({getExp(operandExp)})::int2";

View File

@ -18,7 +18,7 @@ namespace FreeSql.SqlServer {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis(); var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) { if (gentype != operandExp.Type.NullableTypeOrThis()) {
switch (gentype.ToString()) { switch (gentype.ToString()) {
case "System.Boolean": return $"(cast({getExp(operandExp)} as varchar) not in ('0','false'))"; case "System.Boolean": return $"(cast({getExp(operandExp)} as varchar) not in ('0','false'))";
case "System.Byte": return $"cast({getExp(operandExp)} as tinyint)"; case "System.Byte": return $"cast({getExp(operandExp)} as tinyint)";

View File

@ -18,7 +18,7 @@ namespace FreeSql.Sqlite {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis(); var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) { if (gentype != operandExp.Type.NullableTypeOrThis()) {
switch (exp.Type.NullableTypeOrThis().ToString()) { switch (exp.Type.NullableTypeOrThis().ToString()) {
case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))"; case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))";
case "System.Byte": return $"cast({getExp(operandExp)} as int2)"; case "System.Byte": return $"cast({getExp(operandExp)} as int2)";