Procházet zdrojové kódy

support identity key

Lith před 7 měsíci
rodič
revize
3f24d3f837

+ 5 - 0
doc/ReleaseNotes.md

@@ -1 +1,6 @@
 # Vitorm.Excel ReleaseNotes
+
+-----------------------
+# 2.2.0
+
+- support identity key

+ 48 - 0
src/Vitorm.Excel/DbSet.cs

@@ -90,6 +90,14 @@ namespace Vitorm.Excel
 
 
 
+        protected virtual int GetMaxId()
+        {
+            var entities = GetEntities().Select(m => m.entity);
+            if (entities?.Any() != true) return 0;
+            return entities.Max(entity => int.TryParse(entityDescriptor.key.GetValue(entity)?.ToString(), out var id) ? id : 0);
+        }
+
+
         public virtual void AddColumnsIfNotExist()
         {
             var colsToAdd = entityDescriptor.allColumns.Where(col => !columnIndexes.TryGetValue(col.columnName, out var _)).ToList();
@@ -317,6 +325,26 @@ namespace Vitorm.Excel
         {
             AddColumnsIfNotExist();
 
+
+            #region generate identity key if needed
+            if (entityDescriptor.key.isIdentity)
+            {
+                int maxId = GetMaxId();
+
+                entities.ForEach(entity =>
+                {
+                    object keyValue = entityDescriptor.key.GetValue(entity);
+                    var keyIsEmpty = keyValue is null || keyValue.Equals(TypeUtil.DefaultValue(entityDescriptor.key.type));
+                    if (keyIsEmpty)
+                    {
+                        maxId++;
+                        entityDescriptor.key.SetValue(entity, maxId);
+                    }
+                });
+            }
+            #endregion
+
+
             var lastRowIndex = sheet.Dimension?.End.Row ?? 0;
             var range = sheet.Cells[lastRowIndex + 1, 1];
 
@@ -338,6 +366,26 @@ namespace Vitorm.Excel
         {
             AddColumnsIfNotExist();
 
+
+            #region generate identity key if needed
+            if (entityDescriptor.key.isIdentity)
+            {
+                int maxId = GetMaxId();
+
+                entities.ForEach(entity =>
+                {
+                    object keyValue = entityDescriptor.key.GetValue(entity);
+                    var keyIsEmpty = keyValue is null || keyValue.Equals(TypeUtil.DefaultValue(entityDescriptor.key.type));
+                    if (keyIsEmpty)
+                    {
+                        maxId++;
+                        entityDescriptor.key.SetValue(entity, maxId);
+                    }
+                });
+            }
+            #endregion
+
+
             var lastRowIndex = sheet.Dimension?.End.Row ?? 0;
             var range = sheet.Cells[lastRowIndex + 1, 1];
 

+ 10 - 2
test/Vitorm.Excel.MsTest/CommonTest/Truncate_Test.cs

@@ -18,14 +18,22 @@ namespace Vitorm.MsTest.CommonTest
             }
 
             dbContext.Truncate<User>();
-
             DataSource.WaitForUpdate();
-
             // assert
             {
                 var count = dbContext.Query<User>().Count();
                 Assert.AreEqual(0, count);
             }
+
+            dbContext.Add(User.NewUser(1, forAdd: true));
+            DataSource.WaitForUpdate();
+            // assert
+            {
+                var users = dbContext.Query<User>().ToList();
+                Assert.AreEqual(1, users.Count);
+                Assert.AreEqual(1, users[0].id);
+            }
+
         }
 
 

+ 13 - 9
test/Vitorm.Excel.MsTest/DataSource.cs

@@ -13,6 +13,7 @@
 
         [System.ComponentModel.DataAnnotations.Key]
         [System.ComponentModel.DataAnnotations.Schema.Column("userId")]
+        [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
         public int id { get; set; }
         [System.ComponentModel.DataAnnotations.Schema.Column("userName")]
         public string name { get; set; }
@@ -28,7 +29,7 @@
         [System.ComponentModel.DataAnnotations.Schema.NotMapped]
         public string test { get; set; }
 
-        public static User NewUser(int id, bool forAdd = false) => new User { id = id, name = "testUser" + id };
+        public static User NewUser(int id, bool forAdd = false) => new User { id = forAdd ? 0 : id, name = "testUser" + id };
 
         public static List<User> NewUsers(int startId, int count = 1, bool forAdd = false)
         {
@@ -41,13 +42,14 @@
     {
         [System.ComponentModel.DataAnnotations.Key]
         [System.ComponentModel.DataAnnotations.Schema.Column("classId")]
+        [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
         public int id { get; set; }
         [System.ComponentModel.DataAnnotations.Schema.Column("className")]
         public string name { get; set; }
 
         public static List<UserClass> NewClasses(int startId, int count = 1)
         {
-            return Enumerable.Range(startId, count).Select(id => new UserClass { id = id, name = "class" + id }).ToList();
+            return Enumerable.Range(startId, count).Select(id => new UserClass { id = 0, name = "class" + id }).ToList();
         }
     }
 
@@ -82,20 +84,22 @@
                 dbContext.TryCreateTable<User>();
 
                 var users = new List<User> {
-                    new User { id=1, name="u146", fatherId=4, motherId=6 },
-                    new User { id=2, name="u246", fatherId=4, motherId=6 },
-                    new User { id=3, name="u356", fatherId=5, motherId=6 },
-                    new User { id=4, name="u400" },
-                    new User { id=5, name="u500" },
-                    new User { id=6, name="u600" },
+                    new User { name="u146", fatherId=4, motherId=6 },
+                    new User { name="u246", fatherId=4, motherId=6 },
+                    new User { name="u356", fatherId=5, motherId=6 },
+                    new User { name="u400" },
+                    new User { name="u500" },
+                    new User { name="u600" },
                 };
+                dbContext.AddRange(users);
+
                 users.ForEach(user =>
                 {
                     user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id);
                     user.classId = user.id % 2 + 1;
                 });
 
-                dbContext.AddRange(users);
+                dbContext.UpdateRange(users);
             }
             #endregion