From 4e817696cf354b4c0b5b36b71d937a5146f38868 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 14 Apr 2023 19:11:05 +0800 Subject: [PATCH] Synchronization code --- .../SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index cece980da..e67d4dbcc 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -419,7 +419,7 @@ namespace SqlSugar this.Context.DbMaintenance.RenameColumn(tableName, item.OldDbColumnName, item.DbColumnName); isChange = true; } - + var isAddPrimaryKey = false; foreach (var item in entityColumns) { var dbColumn = dbColumns.FirstOrDefault(dc => dc.DbColumnName.Equals(item.DbColumnName, StringComparison.CurrentCultureIgnoreCase)); @@ -431,6 +431,7 @@ namespace SqlSugar var isAdd = item.IsPrimarykey; if (isAdd) { + isAddPrimaryKey = true; this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName); } else @@ -443,7 +444,7 @@ namespace SqlSugar ChangeKey(entityInfo, tableName, item); } } - if (entityColumns.Count(it => it.IsPrimarykey)==1&&dbColumns.Count(it => it.IsPrimarykey) ==0) + if (isAddPrimaryKey==false&&entityColumns.Count(it => it.IsPrimarykey)==1&&dbColumns.Count(it => it.IsPrimarykey) ==0) { var addPk=entityColumns.First(it => it.IsPrimarykey); this.Context.DbMaintenance.AddPrimaryKey(tableName, addPk.DbColumnName);