浏览代码

columnOrder

Lith 10 月之前
父节点
当前提交
0f100b2aa0

+ 7 - 1
src/Vitorm/Entity/DataAnnotations/ColumnDescriptor.cs

@@ -5,7 +5,7 @@ namespace Vitorm.Entity.DataAnnotations
 {
     public class ColumnDescriptor : IColumnDescriptor
     {
-        public ColumnDescriptor(PropertyInfo propertyInfo, string name, bool isKey, bool isIdentity, string databaseType, bool isNullable)
+        public ColumnDescriptor(PropertyInfo propertyInfo, string name, bool isKey, bool isIdentity, string databaseType, bool isNullable, int? columnOrder = null, bool? isIndex = null)
         {
             this.propertyInfo = propertyInfo;
             type = propertyInfo.PropertyType;
@@ -15,6 +15,9 @@ namespace Vitorm.Entity.DataAnnotations
             this.isIdentity = isIdentity;
             this.databaseType = databaseType;
             this.isNullable = isNullable;
+
+            this.columnOrder = columnOrder;
+            this.isIndex = isIndex;
         }
 
         PropertyInfo propertyInfo;
@@ -34,6 +37,9 @@ namespace Vitorm.Entity.DataAnnotations
         /// </summary>
         public bool isNullable { get; private set; }
 
+        public int? columnOrder { get; private set; }
+        public bool? isIndex { get; private set; }
+
         /// <summary>
         /// database provider specific data type of the column the property is mapped to.  example:  varchar(1000)
         /// </summary>

+ 2 - 2
src/Vitorm/Entity/DataAnnotations/EntityDescriptor.cs

@@ -11,9 +11,9 @@ namespace Vitorm.Entity.DataAnnotations
             this.tableName = tableName;
             this.schema = schema;
 
-            this.allColumns = allColumns;
             this.key = allColumns.FirstOrDefault(m => m.isKey);
-            this.columns = allColumns.Where(m => !m.isKey).ToArray();
+            this.columns = allColumns.Where(m => !m.isKey).OrderBy(col => col.columnOrder ?? int.MaxValue).ToArray();
+            this.allColumns = allColumns.OrderBy(col => col.columnOrder ?? int.MaxValue).ToArray();
         }
 
 

+ 3 - 2
src/Vitorm/Entity/DataAnnotations/EntityLoader.cs

@@ -39,10 +39,11 @@ namespace Vitorm.Entity.DataAnnotations
                  bool isKey = propertyInfo.GetCustomAttribute<System.ComponentModel.DataAnnotations.KeyAttribute>() != null;
 
                  // #2 column name and type
-                 string name; string databaseType;
+                 string name; string databaseType; int? columnOrder;
                  var columnAttr = propertyInfo.GetCustomAttribute<System.ComponentModel.DataAnnotations.Schema.ColumnAttribute>();
                  name = columnAttr?.Name ?? propertyInfo.Name;
                  databaseType = columnAttr?.TypeName;
+                 columnOrder = columnAttr?.Order;
 
                  // #3 isIdentity
                  var isIdentity = propertyInfo.GetCustomAttribute<System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedAttribute>()?.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity;
@@ -60,7 +61,7 @@ namespace Vitorm.Entity.DataAnnotations
                      }
                  }
 
-                 return new ColumnDescriptor(propertyInfo, name: name, isKey: isKey, isIdentity: isIdentity, databaseType: databaseType, isNullable: isNullable);
+                 return new ColumnDescriptor(propertyInfo, name: name, isKey: isKey, isIdentity: isIdentity, databaseType: databaseType, isNullable: isNullable, columnOrder: columnOrder);
              }).Where(column => column != null).ToArray();
 
             return new EntityDescriptor(entityType, allColumns, tableName, schema);

+ 4 - 3
src/Vitorm/Entity/IColumnDescriptor.cs

@@ -17,17 +17,18 @@ namespace Vitorm.Entity
         /// </summary>
         bool isNullable { get; }
 
+        int? columnOrder { get; }
+        bool? isIndex { get; }
+
         /// <summary>
         /// database provider specific data type of the column the property is mapped to.  example:  varchar(1000)
         /// </summary>
         string databaseType { get; }
 
-    
 
         void SetValue(object entity, object value);
         object GetValue(object entity);
 
-        //bool? isIndex { get; }
-        //int? columnOrder { get; }
+
     }
 }

+ 3 - 2
test/Vitorm.Sqlite.MsTest/CommonTest/DbContext_Test.cs

@@ -102,7 +102,7 @@ namespace Vitorm.MsTest.CommonTest
         public class CustomEntityLoader : IEntityLoader
         {
             public void CleanCache() { }
-            public IEntityDescriptor LoadDescriptor(Type entityType) => LoadFromType(entityType);            
+            public IEntityDescriptor LoadDescriptor(Type entityType) => LoadFromType(entityType);
 
             public static bool GetTableName(Type entityType, out string tableName, out string schema)
             {
@@ -130,6 +130,7 @@ namespace Vitorm.MsTest.CommonTest
                         // #2 column name and type
                         var name = properties.FirstOrDefault(attr => attr.name == "ColumnName")?.value ?? propertyInfo.Name;
                         var databaseType = properties.FirstOrDefault(attr => attr.name == "TypeName")?.value;
+                        int? columnOrder = int.TryParse(properties.FirstOrDefault(attr => attr.name == "ColumnOrder")?.value, out var order) ? order : null;
 
                         // #3 isIdentity
                         var isIdentity = labels.Any(m => m.label == "Identity");
@@ -147,7 +148,7 @@ namespace Vitorm.MsTest.CommonTest
                             }
                         }
 
-                        return new ColumnDescriptor(propertyInfo, name: name, isKey: isKey, isIdentity: isIdentity, databaseType: databaseType, isNullable: isNullable);
+                        return new ColumnDescriptor(propertyInfo, name: name, isKey: isKey, isIdentity: isIdentity, databaseType: databaseType, isNullable: isNullable, columnOrder: columnOrder);
                     }).Where(column => column != null).ToArray();
 
                 return new EntityDescriptor(entityType, allColumns, tableName, schema);