diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs index dac379001..986d0fcfc 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs @@ -227,6 +227,17 @@ namespace SqlSugar protected virtual void Execute(Type entityType) { var entityInfo = this.Context.EntityMaintenance.GetEntityInfoNoCache(entityType); + if (entityInfo.Discrimator.HasValue()) + { + Check.ExceptionEasy(!Regex.IsMatch(entityInfo.Discrimator, @"^(?:\w+:\w+)(?:,\w+:\w+)*$"), "The format should be type:cat for this type, and if there are multiple, it can be FieldName:cat,FieldName2:dog ", "格式错误应该是type:cat这种格式,如果是多个可以FieldName:cat,FieldName2:dog,不要有空格"); + var array = entityInfo.Discrimator.Split(','); + foreach (var disItem in array) + { + var name = disItem.Split(':').First(); + var value = disItem.Split(':').Last(); + entityInfo.Columns.Add(new EntityColumnInfo() { PropertyInfo=typeof(DiscriminatorObject).GetProperty(nameof(DiscriminatorObject.FieldName)),IsOnlyIgnoreUpdate = true, DbColumnName = name, UnderType = typeof(string), PropertyName = name, Length = 50 }); + } + } if (this.MappingTables.ContainsKey(entityType)) { entityInfo.DbTableName = this.MappingTables[entityType]; diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/DynamicBuilder/Helper.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/DynamicBuilder/Helper.cs index a8cf8b4c0..6dbb6b911 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/DynamicBuilder/Helper.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/DynamicBuilder/Helper.cs @@ -39,13 +39,17 @@ namespace SqlSugar attributeType.GetProperty(nameof(SugarTable.TableName)), attributeType.GetProperty(nameof(SugarTable.TableDescription)) , attributeType.GetProperty(nameof(SugarTable.IsDisabledUpdateAll)) , - attributeType.GetProperty(nameof(SugarTable.IsDisabledDelete)) + attributeType.GetProperty(nameof(SugarTable.IsDisabledDelete)), + attributeType.GetProperty(nameof(SugarTable.IsCreateTableFiledSort)), + attributeType.GetProperty(nameof(SugarTable.Discrimator)) } , new object[] { sugarTable.TableName, sugarTable.TableDescription , sugarTable.IsDisabledUpdateAll, - sugarTable.IsDisabledDelete + sugarTable.IsDisabledDelete, + sugarTable.IsCreateTableFiledSort, + sugarTable.Discrimator }); return attributeBuilder; } diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs index 1c679b13f..ed1e5d80f 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs @@ -62,6 +62,7 @@ namespace SqlSugar result.IsDisabledUpdateAll = sugarTable.IsDisabledUpdateAll; result.IsDisabledDelete = sugarTable.IsDisabledDelete; result.IsCreateTableFiledSort = sugarTable.IsCreateTableFiledSort; + result.Discrimator = sugarTable.Discrimator; } var indexs = type.GetCustomAttributes(typeof(SugarIndexAttribute)); if (indexs != null && indexs.Any()) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs index 6152fc7e3..552f9ecfc 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace SqlSugar @@ -292,6 +293,17 @@ namespace SqlSugar } insertItem.Add(columnInfo); } + if (EntityInfo.Discrimator.HasValue()) + { + Check.ExceptionEasy(!Regex.IsMatch(EntityInfo.Discrimator, @"^(?:\w+:\w+)(?:,\w+:\w+)*$"), "The format should be type:cat for this type, and if there are multiple, it can be FieldName:cat,FieldName2:dog ", "格式错误应该是type:cat这种格式,如果是多个可以FieldName:cat,FieldName2:dog,不要有空格"); + var array = EntityInfo.Discrimator.Split(','); + foreach (var disItem in array) + { + var name = disItem.Split(':').First(); + var value = disItem.Split(':').Last(); + insertItem.Add(new DbColumnInfo() { DbColumnName = name, PropertyName = name, PropertyType = typeof(string), Value = value }); + } + } } private string GetDbColumnName(string propertyName) diff --git a/Src/Asp.NetCore2/SqlSugar/Entities/DiscriminatorObject .cs b/Src/Asp.NetCore2/SqlSugar/Entities/DiscriminatorObject .cs new file mode 100644 index 000000000..a45102e52 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/Entities/DiscriminatorObject .cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public struct DiscriminatorObject + { + public string FieldName { get; set; } + public string FieldValue { get; set; } + } +} diff --git a/Src/Asp.NetCore2/SqlSugar/Entities/EntityInfo.cs b/Src/Asp.NetCore2/SqlSugar/Entities/EntityInfo.cs index 46833ac66..a98878631 100644 --- a/Src/Asp.NetCore2/SqlSugar/Entities/EntityInfo.cs +++ b/Src/Asp.NetCore2/SqlSugar/Entities/EntityInfo.cs @@ -19,5 +19,6 @@ namespace SqlSugar public bool IsDisabledUpdateAll { get; set; } public List Indexs { get; set; } public bool IsCreateTableFiledSort { get; set; } + public string Discrimator { get; set; } } } diff --git a/Src/Asp.NetCore2/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs b/Src/Asp.NetCore2/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs index 75da9a602..7e1e0fbd4 100644 --- a/Src/Asp.NetCore2/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs +++ b/Src/Asp.NetCore2/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs @@ -14,6 +14,7 @@ namespace SqlSugar public bool IsDisabledDelete { get; set; } public bool IsDisabledUpdateAll { get; set; } public bool IsCreateTableFiledSort { get; set; } + public string Discrimator { get; set; } public SugarTable(string tableName) { this.TableName = tableName; }