From 531735a187641a821e85804152b840977c5f8096 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sat, 10 Dec 2022 13:24:35 +0800 Subject: [PATCH] Synchronization code --- .../CodeFirstProvider/CodeFirstProvider.cs | 4 ++++ .../SqlSugar/Entities/ConnMoreSettings.cs | 1 + .../DbMaintenance/SqlServerDbMaintenance.cs | 19 +++++++++++++++++++ Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs | 12 +++--------- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index c88187e78..dbdf28698 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -573,6 +573,10 @@ namespace SqlSugar { return false; } + if (properyTypeName?.ToLower() == "varchar" && dataType?.ToLower() == "nvarchar") + { + return false; + } if (properyTypeName?.ToLower() == "number" && dataType?.ToLower() == "decimal") { return false; diff --git a/Src/Asp.Net/SqlSugar/Entities/ConnMoreSettings.cs b/Src/Asp.Net/SqlSugar/Entities/ConnMoreSettings.cs index d36a02442..7a2c3a532 100644 --- a/Src/Asp.Net/SqlSugar/Entities/ConnMoreSettings.cs +++ b/Src/Asp.Net/SqlSugar/Entities/ConnMoreSettings.cs @@ -18,5 +18,6 @@ namespace SqlSugar public bool? TableEnumIsString { get; set; } public DateTime? DbMinDate { get; set; } = Convert.ToDateTime("1900-01-01"); public bool IsNoReadXmlDescription { get; set; } + public bool SqlServerCodeFirstNvarchar { get; set; } } } diff --git a/Src/Asp.Net/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs b/Src/Asp.Net/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs index a1f58cfbc..1fa04674f 100644 --- a/Src/Asp.Net/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs +++ b/Src/Asp.Net/SqlSugar/Realization/SqlServer/DbMaintenance/SqlServerDbMaintenance.cs @@ -321,6 +321,17 @@ namespace SqlSugar #endregion #region Methods + public override bool UpdateColumn(string tableName, DbColumnInfo column) + { + if (column.DataType != null && this.Context.CurrentConnectionConfig?.MoreSettings?.SqlServerCodeFirstNvarchar == true) + { + if (!column.DataType.ToLower().Contains("nvarchar")) + { + column.DataType = column.DataType.ToLower().Replace("varchar", "nvarchar"); + } + } + return base.UpdateColumn(tableName, column); + } public override bool IsAnyTable(string tableName, bool isCache = true) { if (tableName.Contains(".")) @@ -536,6 +547,14 @@ namespace SqlSugar item.DecimalDigits = 4; item.Length = 18; } + else if (item.DataType != null && this.Context.CurrentConnectionConfig?.MoreSettings?.SqlServerCodeFirstNvarchar == true) + { + if (!item.DataType.ToLower().Contains("nvarchar")) + { + item.DataType = item.DataType.ToLower().Replace("varchar", "nvarchar"); + } + } + } string sql = GetCreateTableSql(tableName, columns); this.Context.Ado.ExecuteCommand(sql); diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs index 4105e3be8..02a38b5d0 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs @@ -17,13 +17,6 @@ namespace SqlSugar { public class UtilMethods { - public static string ToUnderLineNew(string str) - { - if(!str.Contains("_")) - return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())); - else - return str; - } public static string ToUnderLine(string str, bool isToUpper = false) { if (isToUpper) @@ -181,8 +174,9 @@ namespace SqlSugar TableEnumIsString = it.MoreSettings.TableEnumIsString, DisableMillisecond = it.MoreSettings.DisableMillisecond, DbMinDate=it.MoreSettings.DbMinDate, - IsNoReadXmlDescription=it.MoreSettings.IsNoReadXmlDescription - + IsNoReadXmlDescription=it.MoreSettings.IsNoReadXmlDescription, + SqlServerCodeFirstNvarchar=it.MoreSettings.SqlServerCodeFirstNvarchar + }, SqlMiddle = it.SqlMiddle == null ? null : new SqlMiddle {