mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	修复 SqlServer CodeFirst 迁移结构时,因日期默认值的语法错误
This commit is contained in:
		@@ -33,15 +33,37 @@ namespace FreeSql.Tests.SqlServer {
 | 
			
		||||
 | 
			
		||||
		[Fact]
 | 
			
		||||
		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 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 {
 | 
			
		||||
			public int Id { get; set; }
 | 
			
		||||
			public int ParentId { get; set; }
 | 
			
		||||
			public xxx Parent { get; set; }
 | 
			
		||||
			public string Title { get; set; }
 | 
			
		||||
			public string Url { get; set; }
 | 
			
		||||
			public DateTime Create_time { get; set; }
 | 
			
		||||
 
 | 
			
		||||
@@ -242,10 +242,10 @@ use " + database, tboldname ?? tbname);
 | 
			
		||||
							if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false)
 | 
			
		||||
								insertvalue = $"cast({insertvalue} as {tbcol.Attribute.DbType.Split(' ').First()})";
 | 
			
		||||
							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)
 | 
			
		||||
							insertvalue = _commonUtils.FormatSql("{0}", tbcol.Attribute.DbDefautValue);
 | 
			
		||||
						sb.Append(insertvalue).Append(", ");
 | 
			
		||||
							insertvalue = _commonUtils.FormatSql("{0}", GetTransferDbDefaultValue(tbcol));
 | 
			
		||||
						sb.Append(insertvalue.Replace("'", "''")).Append(", ");
 | 
			
		||||
					}
 | 
			
		||||
					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");
 | 
			
		||||
@@ -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();
 | 
			
		||||
		ConcurrentDictionary<string, bool> dicSyced = new ConcurrentDictionary<string, bool>();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user