Browse Source

CodeFirst (DateOnly TimeOnly)

pull/31/MERGE
sunkaixuan 2 years ago
parent
commit
9eaae31945
  1. 12
      Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
  2. 9
      Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs
  3. 7
      Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs

12
Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs

@ -241,7 +241,17 @@ namespace SqlSugar
var tableName = GetTableName(entityInfo); var tableName = GetTableName(entityInfo);
this.Context.MappingTables.Add(entityInfo.EntityName, tableName); this.Context.MappingTables.Add(entityInfo.EntityName, tableName);
entityInfo.DbTableName = tableName; entityInfo.DbTableName = tableName;
entityInfo.Columns.ForEach(it => { it.DbTableName = tableName; }); entityInfo.Columns.ForEach(it => {
it.DbTableName = tableName;
if (it.UnderType?.Name == "DateOnly" && it.DataType == null)
{
it.DataType = "Date";
}
if (it.UnderType?.Name == "TimeOnly" && it.DataType == null)
{
it.DataType = "Time";
}
});
var isAny = this.Context.DbMaintenance.IsAnyTable(tableName, false); var isAny = this.Context.DbMaintenance.IsAnyTable(tableName, false);
if (isAny && entityInfo.IsDisabledUpdateAll) if (isAny && entityInfo.IsDisabledUpdateAll)
{ {

9
Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs

@ -319,7 +319,14 @@ namespace SqlSugar
{ {
var timeSpan = UtilMethods.DateOnlyToDateTime(columnInfo.Value); var timeSpan = UtilMethods.DateOnlyToDateTime(columnInfo.Value);
var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex; var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex;
this.Parameters.Add(new SugarParameter(pname,Convert.ToDateTime(timeSpan))); if (timeSpan == null)
{
this.Parameters.Add(new SugarParameter(pname, null) { DbType=System.Data.DbType.Date });
}
else
{
this.Parameters.Add(new SugarParameter(pname, Convert.ToDateTime(timeSpan)));
}
GetDbColumnIndex++; GetDbColumnIndex++;
return pname; return pname;
} }

7
Src/Asp.NetCore2/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs

@ -417,7 +417,14 @@ namespace SqlSugar
{ {
var timeSpan = UtilMethods.TimeOnlyToTimeSpan(columnInfo.Value); var timeSpan = UtilMethods.TimeOnlyToTimeSpan(columnInfo.Value);
var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex; var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex;
if (timeSpan == null)
{
this.Parameters.Add(new SugarParameter(pname, null) { DbType = System.Data.DbType.Date });
}
else
{
this.Parameters.Add(new SugarParameter(pname, timeSpan)); this.Parameters.Add(new SugarParameter(pname, timeSpan));
}
GetDbColumnIndex++; GetDbColumnIndex++;
return pname; return pname;
} }

Loading…
Cancel
Save