From d84e6155b6a40a5c511f19756aacd6721a6d1ef5 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 14 Apr 2023 19:10:04 +0800 Subject: [PATCH] Update codefirst --- .../SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index cece980da..e67d4dbcc 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.NetCore2/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);