From 4def1b383885aecdf55623381a914ef5bcd43cd0 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期四, 31 十月 2024 14:33:11 +0800
Subject: [PATCH] 添加序列特性,可自动赋值

---
 WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs |   28 ++++++++++++++++++++++++++--
 1 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs b/WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs
index bebf842..f5d31be 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_Core/Seed/DBSeed.cs
@@ -11,6 +11,7 @@
 using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEAWCS_Core.Attributes;
 using WIDESEAWCS_Core.Const;
 using WIDESEAWCS_Core.DB;
 using WIDESEAWCS_Core.Helper;
@@ -106,7 +107,19 @@
                                     for (int i = 0; i < propertyInfos.Count; i++)
                                     {
                                         PropertyInfo propertyInfo = propertyInfos[i];
-                                        SugarColumn sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>();
+
+                                        SequenceAttirbute? sequenceAttirbute = propertyInfo.GetCustomAttribute<SequenceAttirbute>();
+                                        if (sequenceAttirbute != null)
+                                        {
+                                            int count = dbContext.Db.Ado.GetScalar($"SELECT COUNT(*) FROM sys.sequences WHERE name = '{sequenceAttirbute.SequenceName}'").ObjToInt();
+                                            if (count == 0)
+                                            {
+                                                string sql = $"CREATE SEQUENCE {sequenceAttirbute.SequenceName} AS [int] START WITH {sequenceAttirbute.StartWith} INCREMENT BY {sequenceAttirbute.Increment} MINVALUE {sequenceAttirbute.SeqMinValue} MAXVALUE {sequenceAttirbute.SeqMaxValue} {(sequenceAttirbute.IsCycle ? "CYCLE" : "")} CACHE";
+                                                dbContext.Db.Ado.ExecuteCommand(sql);
+                                            }
+                                        }
+
+                                        SugarColumn? sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>();
                                         if (sugarColumn != null)
                                         {
                                             if (!sugarColumn.IsIgnore)
@@ -151,7 +164,18 @@
                             for (int i = 0; i < propertyInfos.Count; i++)
                             {
                                 PropertyInfo propertyInfo = propertyInfos[i];
-                                SugarColumn sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>();
+
+                                SequenceAttirbute? sequenceAttirbute = propertyInfo.GetCustomAttribute<SequenceAttirbute>();
+                                if (sequenceAttirbute != null)
+                                {
+                                        int count = dbContext.Db.Ado.GetScalar($"SELECT COUNT(*) FROM sys.sequences WHERE name = '{sequenceAttirbute.SequenceName}'").ObjToInt();
+                                        if (count == 0)
+                                        {
+                                            string sql = $"CREATE SEQUENCE {sequenceAttirbute.SequenceName} AS [int] START WITH {sequenceAttirbute.StartWith} INCREMENT BY {sequenceAttirbute.Increment} MINVALUE {sequenceAttirbute.SeqMinValue} MAXVALUE {sequenceAttirbute.SeqMaxValue} {(sequenceAttirbute.IsCycle ? "CYCLE" : "")} CACHE";
+                                        dbContext.Db.Ado.ExecuteCommand(sql);
+                                        }
+                                }
+                                SugarColumn? sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>();
                                 if (sugarColumn != null)
                                 {
                                     if (!sugarColumn.IsIgnore)

--
Gitblit v1.9.3