修复 SqlServer CodeFirst 迁移结构时,因日期默认值的语法错误

This commit is contained in:
28810 2019-02-27 17:50:50 +08:00
parent 9d87c69fb2
commit 3ec5081d1b
2 changed files with 38 additions and 4 deletions

View File

@ -33,15 +33,37 @@ namespace FreeSql.Tests.SqlServer {
[Fact] [Fact]
public void Query() { public void Query() {
//var tt1 = g.sqlserver.Select<xxx>()
// .LeftJoin(a => a.ParentId == a.Parent.Id)
// .ToSql(a => new { a.Id, a.Title });
//var tt2result = g.sqlserver.Select<xxx>()
// .LeftJoin(a => a.ParentId == a.Parent.Id)
// .ToList(a => new { a.Id, a.Title });
//var tt = g.sqlserver.Select<xxx>()
// .LeftJoin<xxx>((a, b) => b.Id == a.Id)
// .ToSql(a => new { a.Id, a.Title });
//var ttresult = g.sqlserver.Select<xxx>()
// .LeftJoin<xxx>((a, b) => b.Id == a.Id)
// .ToList(a => new { a.Id, a.Title });
var tn = g.sqlserver.Select<xxx>().Where(a => a.Id > 0).Where(b => b.Title != null).ToList(a => a.Id);
var t3 = g.sqlserver.Ado.Query<xxx>("select * from song"); var t3 = g.sqlserver.Ado.Query<xxx>("select * from song");
var t4 = g.sqlserver.Ado.Query<(int, string, string, DateTime)>("select * from song"); var t4 = g.sqlserver.Ado.Query<(int, string, string, DateTime)>("select * from song");
var t5 = g.sqlserver.Ado.Query<dynamic>("select * from song"); var t5 = g.sqlserver.Ado.Query<dynamic>(System.Data.CommandType.Text, "select * from song where Id = @Id",
new System.Data.SqlClient.SqlParameter("Id", 1));
} }
class xxx { class xxx {
public int Id { get; set; } public int Id { get; set; }
public int ParentId { get; set; }
public xxx Parent { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Url { get; set; } public string Url { get; set; }
public DateTime Create_time { get; set; } public DateTime Create_time { get; set; }

View File

@ -242,10 +242,10 @@ use " + database, tboldname ?? tbname);
if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false) if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false)
insertvalue = $"cast({insertvalue} as {tbcol.Attribute.DbType.Split(' ').First()})"; insertvalue = $"cast({insertvalue} as {tbcol.Attribute.DbType.Split(' ').First()})";
if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable) if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable)
insertvalue = $"isnull({insertvalue},{_commonUtils.FormatSql("{0}", tbcol.Attribute.DbDefautValue)})"; insertvalue = $"isnull({insertvalue},{_commonUtils.FormatSql("{0}", GetTransferDbDefaultValue(tbcol))})";
} else if (tbcol.Attribute.IsNullable == false) } else if (tbcol.Attribute.IsNullable == false)
insertvalue = _commonUtils.FormatSql("{0}", tbcol.Attribute.DbDefautValue); insertvalue = _commonUtils.FormatSql("{0}", GetTransferDbDefaultValue(tbcol));
sb.Append(insertvalue).Append(", "); sb.Append(insertvalue.Replace("'", "''")).Append(", ");
} }
sb.Remove(sb.Length - 2, 2).Append(" FROM ").Append(tablename).Append(" WITH (HOLDLOCK TABLOCKX)');\r\n"); sb.Remove(sb.Length - 2, 2).Append(" FROM ").Append(tablename).Append(" WITH (HOLDLOCK TABLOCKX)');\r\n");
if (idents) sb.Append("SET IDENTITY_INSERT ").Append(tmptablename).Append(" OFF;\r\n"); if (idents) sb.Append("SET IDENTITY_INSERT ").Append(tmptablename).Append(" OFF;\r\n");
@ -263,6 +263,18 @@ use " + database, tboldname ?? tbname);
} }
} }
} }
object GetTransferDbDefaultValue(ColumnInfo col) {
var ddv = col.Attribute.DbDefautValue;
if (ddv == null) return ddv;
if (ddv is DateTime || ddv is DateTime?) {
var dt = (DateTime)ddv;
if (col.Attribute.DbType.Contains("SMALLDATETIME") && dt < new DateTime(1900, 1, 1)) ddv = new DateTime(1900, 1, 1);
else if (col.Attribute.DbType.Contains("DATETIME") && dt < new DateTime(1753, 1, 1)) ddv = new DateTime(1753, 1, 1);
else if (col.Attribute.DbType.Contains("DATE") && dt < new DateTime(0001, 1, 1)) ddv = new DateTime(0001, 1, 1);
}
return ddv;
}
static object syncStructureLock = new object(); static object syncStructureLock = new object();
ConcurrentDictionary<string, bool> dicSyced = new ConcurrentDictionary<string, bool>(); ConcurrentDictionary<string, bool> dicSyced = new ConcurrentDictionary<string, bool>();