From 97975b5ab55ae8896b993d43fa337fe218bd7021 Mon Sep 17 00:00:00 2001 From: 2881099 <2881099@qq.com> Date: Wed, 25 May 2022 15:58:59 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=20pgsql=20varchar(120)?= =?UTF-8?q?=20CodeFirst=20=E8=BF=81=E7=A7=BB=E4=B8=8D=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/Program.cs | 79 ++++++++++++++++++- Examples/base_entity/base_entity.xml | 35 ++++++++ .../KingbaseESCodeFirst.cs | 14 +++- .../KingbaseES/OdbcKingbaseESCodeFirst.cs | 14 +++- .../PostgreSQL/OdbcPostgreSQLCodeFirst.cs | 14 +++- .../PostgreSQLCodeFirst.cs | 11 ++- .../ShenTongCodeFirst.cs | 14 +++- 7 files changed, 173 insertions(+), 8 deletions(-) diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index d9bbe1a7..40be97a9 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -180,11 +180,80 @@ namespace base_entity [Column(DbType = "varchar(128)")] public string img { get; set; } } + + public class SongRepository : BaseRepository + { + public SongRepository(IFreeSql fsql) : base(fsql, null, null) + { + //fsql.CodeFirst.Entity(a => + // { + // //a.Property(b => b.Id).DbType("varchar(100)"); + // a.Property(b => b.UserId).Stringlength(120); + // a.Property(b=>b.UserId). + // }); + fsql.CodeFirst + .ConfigEntity(a => + { + a.Property(b => b.UserId).StringLength(120); + }); + //var info= fsql.CodeFirst.GetTableByEntity(typeof(TUserImg)); + var sql = fsql.CodeFirst.GetComparisonDDLStatements(); + var t1 = fsql.CodeFirst.GetComparisonDDLStatements(typeof(TUserImg), "TUserImg"); + fsql.CodeFirst.SyncStructure(); ;//同步表结构 + + var debug = sql; + } + + //在这里增加 CURD 以外的方法 + } + public interface IEntity + { + + } + public partial class TUserImg : IEntity + { + + /// + ///主键 + /// + public string Id { get; set; } + + /// + ///企业 + /// + public string EnterpriseId { get; set; } + + /// + ///用户id + /// + public string UserId { get; set; } + + /// + ///图片 + /// + public string Img { get; set; } + + /// + ///创建人Id + /// + public string CId { get; set; } + + /// + ///创建人 + /// + public string CName { get; set; } + + /// + ///创建日期 + /// + public DateTime CTime { get; set; } + } + static void Main(string[] args) { #region 初始化 IFreeSql var fsql = new FreeSql.FreeSqlBuilder() - .UseAutoSyncStructure(true) + .UseAutoSyncStructure(false) .UseNoneCommandParameter(true) .UseConnectionString(FreeSql.DataType.Sqlite, "data source=test1.db;max pool size=5") @@ -199,8 +268,8 @@ namespace base_entity //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true") - //.UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2") - //.UseNameConvert(FreeSql.Internal.NameConvertType.ToLower) + .UseConnectionString(FreeSql.DataType.PostgreSQL, "Host=192.168.164.10;Port=5432;Username=postgres;Password=123456;Database=tedb;Pooling=true;Maximum Pool Size=2") + .UseNameConvert(FreeSql.Internal.NameConvertType.ToLower) //.UseConnectionString(FreeSql.DataType.Oracle, "user id=user1;password=123456;data source=//127.0.0.1:1521/XE;Pooling=true;Max Pool Size=2") //.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper) @@ -225,6 +294,10 @@ namespace base_entity BaseEntity.Initialization(fsql, () => _asyncUow.Value); #endregion + fsql.Select(); + + var srepo = new SongRepository(fsql); + var sql122234 = fsql.CodeFirst.GetComparisonDDLStatements(); if (fsql.Ado.DataType == DataType.PostgreSQL) diff --git a/Examples/base_entity/base_entity.xml b/Examples/base_entity/base_entity.xml index 1241b5ac..44737d77 100644 --- a/Examples/base_entity/base_entity.xml +++ b/Examples/base_entity/base_entity.xml @@ -29,6 +29,41 @@ 描述 + + + 主键 + + + + + 企业 + + + + + 用户id + + + + + 图片 + + + + + 创建人Id + + + + + 创建人 + + + + + 创建日期 + + 角色表 diff --git a/Providers/FreeSql.Provider.KingbaseES/KingbaseESCodeFirst.cs b/Providers/FreeSql.Provider.KingbaseES/KingbaseESCodeFirst.cs index b641c526..5c4dff19 100644 --- a/Providers/FreeSql.Provider.KingbaseES/KingbaseESCodeFirst.cs +++ b/Providers/FreeSql.Provider.KingbaseES/KingbaseESCodeFirst.cs @@ -225,7 +225,19 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname); string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol)) { var isCommentChanged = tbstructcol.comment != (tbcol.Comment ?? ""); - if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false || + var sqlTypeSize = tbstructcol.sqlType; + if (sqlTypeSize.Contains("(") == false) + { + switch (sqlTypeSize.ToLower()) + { + case "bit": + case "varbit": + case "bpchar": + case "varchar": + sqlTypeSize = $"{sqlTypeSize}({tbstructcol.max_length})"; break; + } + } + if (tbcol.Attribute.DbType.StartsWith(sqlTypeSize, StringComparison.CurrentCultureIgnoreCase) == false || tbcol.Attribute.DbType.Contains("[]") != (tbstructcol.attndims > 0)) sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" TYPE ").Append(tbcol.Attribute.DbType.Split(' ').First()).Append(";\r\n"); if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable) diff --git a/Providers/FreeSql.Provider.Odbc/KingbaseES/OdbcKingbaseESCodeFirst.cs b/Providers/FreeSql.Provider.Odbc/KingbaseES/OdbcKingbaseESCodeFirst.cs index 1bdb9b64..59094729 100644 --- a/Providers/FreeSql.Provider.Odbc/KingbaseES/OdbcKingbaseESCodeFirst.cs +++ b/Providers/FreeSql.Provider.Odbc/KingbaseES/OdbcKingbaseESCodeFirst.cs @@ -229,7 +229,19 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname); string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol)) { var isCommentChanged = tbstructcol.comment != (tbcol.Comment ?? ""); - if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false || + var sqlTypeSize = tbstructcol.sqlType; + if (sqlTypeSize.Contains("(") == false) + { + switch (sqlTypeSize.ToLower()) + { + case "bit": + case "varbit": + case "bpchar": + case "varchar": + sqlTypeSize = $"{sqlTypeSize}({tbstructcol.max_length})"; break; + } + } + if (tbcol.Attribute.DbType.StartsWith(sqlTypeSize, StringComparison.CurrentCultureIgnoreCase) == false || tbcol.Attribute.DbType.Contains("[]") != (tbstructcol.attndims > 0)) sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" TYPE ").Append(tbcol.Attribute.DbType.Split(' ').First()).Append(";\r\n"); if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable) diff --git a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs index 9f9b0d50..8441fcd6 100644 --- a/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs +++ b/Providers/FreeSql.Provider.Odbc/PostgreSQL/OdbcPostgreSQLCodeFirst.cs @@ -241,7 +241,19 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname); string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol)) { var isCommentChanged = tbstructcol.comment != (tbcol.Comment ?? ""); - if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false || + var sqlTypeSize = tbstructcol.sqlType; + if (sqlTypeSize.Contains("(") == false) + { + switch (sqlTypeSize.ToLower()) + { + case "bit": + case "varbit": + case "bpchar": + case "varchar": + sqlTypeSize = $"{sqlTypeSize}({tbstructcol.max_length})"; break; + } + } + if (tbcol.Attribute.DbType.StartsWith(sqlTypeSize, StringComparison.CurrentCultureIgnoreCase) == false || tbcol.Attribute.DbType.Contains("[]") != (tbstructcol.attndims > 0)) sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" TYPE ").Append(tbcol.Attribute.DbType.Split(' ').First()).Append(";\r\n"); if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable) diff --git a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs index da13e9de..0776863d 100644 --- a/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs +++ b/Providers/FreeSql.Provider.PostgreSQL/PostgreSQLCodeFirst.cs @@ -290,7 +290,16 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname); string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol)) { var isCommentChanged = tbstructcol.comment != (tbcol.Comment ?? ""); - if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false || + var sqlTypeSize = tbstructcol.sqlType; + if (sqlTypeSize.Contains("(") == false) + { + switch (sqlTypeSize.ToLower()) + { + case "bit": case "varbit": case "bpchar": case "varchar": + sqlTypeSize = $"{sqlTypeSize}({tbstructcol.max_length})"; break; + } + } + if (tbcol.Attribute.DbType.StartsWith(sqlTypeSize, StringComparison.CurrentCultureIgnoreCase) == false || tbcol.Attribute.DbType.Contains("[]") != (tbstructcol.attndims > 0)) sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" TYPE ").Append(tbcol.Attribute.DbType.Split(' ').First()).Append(";\r\n"); if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable) diff --git a/Providers/FreeSql.Provider.ShenTong/ShenTongCodeFirst.cs b/Providers/FreeSql.Provider.ShenTong/ShenTongCodeFirst.cs index 075c7ad3..38bc584e 100644 --- a/Providers/FreeSql.Provider.ShenTong/ShenTongCodeFirst.cs +++ b/Providers/FreeSql.Provider.ShenTong/ShenTongCodeFirst.cs @@ -241,7 +241,19 @@ where ns.nspname = {0} and c.relname = {1}", tboldname ?? tbname); string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol)) { var isCommentChanged = tbstructcol.comment != (tbcol.Comment ?? ""); - if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false || + var sqlTypeSize = tbstructcol.sqlType; + if (sqlTypeSize.Contains("(") == false) + { + switch (sqlTypeSize.ToLower()) + { + case "bit": + case "varbit": + case "bpchar": + case "varchar": + sqlTypeSize = $"{sqlTypeSize}({tbstructcol.max_length})"; break; + } + } + if (tbcol.Attribute.DbType.StartsWith(sqlTypeSize, StringComparison.CurrentCultureIgnoreCase) == false || tbcol.Attribute.DbType.Contains("[]") != (tbstructcol.attndims > 0)) sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER TYPE ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(tbcol.Attribute.DbType.Split(' ').First()).Append(";\r\n"); if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable)