Browse Source

Support Discrimator

pull/40/head
sunkaixuan 1 year ago
parent
commit
615d31a311
  1. 11
      Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs
  2. 8
      Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs
  3. 1
      Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs
  4. 12
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs
  5. 14
      Src/Asp.Net/SqlSugar/Entities/DiscriminatorObject .cs
  6. 1
      Src/Asp.Net/SqlSugar/Entities/EntityInfo.cs
  7. 1
      Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs
  8. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

11
Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/CodeFirstProvider.cs

@ -227,6 +227,17 @@ namespace SqlSugar
protected virtual void Execute(Type entityType) protected virtual void Execute(Type entityType)
{ {
var entityInfo = this.Context.EntityMaintenance.GetEntityInfoNoCache(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() { IsOnlyIgnoreUpdate = true, DbColumnName = name, UnderType = typeof(string), PropertyName = name, Length = 50 });
}
}
if (this.MappingTables.ContainsKey(entityType)) if (this.MappingTables.ContainsKey(entityType))
{ {
entityInfo.DbTableName = this.MappingTables[entityType]; entityInfo.DbTableName = this.MappingTables[entityType];

8
Src/Asp.Net/SqlSugar/Abstract/DynamicBuilder/Helper.cs

@ -39,13 +39,17 @@ namespace SqlSugar
attributeType.GetProperty(nameof(SugarTable.TableName)), attributeType.GetProperty(nameof(SugarTable.TableName)),
attributeType.GetProperty(nameof(SugarTable.TableDescription)) , attributeType.GetProperty(nameof(SugarTable.TableDescription)) ,
attributeType.GetProperty(nameof(SugarTable.IsDisabledUpdateAll)) , 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[] { , new object[] {
sugarTable.TableName, sugarTable.TableName,
sugarTable.TableDescription , sugarTable.TableDescription ,
sugarTable.IsDisabledUpdateAll, sugarTable.IsDisabledUpdateAll,
sugarTable.IsDisabledDelete sugarTable.IsDisabledDelete,
sugarTable.IsCreateTableFiledSort,
sugarTable.Discrimator
}); });
return attributeBuilder; return attributeBuilder;
} }

1
Src/Asp.Net/SqlSugar/Abstract/EntityMaintenance/EntityMaintenance.cs

@ -62,6 +62,7 @@ namespace SqlSugar
result.IsDisabledUpdateAll = sugarTable.IsDisabledUpdateAll; result.IsDisabledUpdateAll = sugarTable.IsDisabledUpdateAll;
result.IsDisabledDelete = sugarTable.IsDisabledDelete; result.IsDisabledDelete = sugarTable.IsDisabledDelete;
result.IsCreateTableFiledSort = sugarTable.IsCreateTableFiledSort; result.IsCreateTableFiledSort = sugarTable.IsCreateTableFiledSort;
result.Discrimator = sugarTable.Discrimator;
} }
var indexs = type.GetCustomAttributes(typeof(SugarIndexAttribute)); var indexs = type.GetCustomAttributes(typeof(SugarIndexAttribute));
if (indexs != null && indexs.Any()) if (indexs != null && indexs.Any())

12
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs

@ -5,6 +5,7 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace SqlSugar namespace SqlSugar
@ -292,6 +293,17 @@ namespace SqlSugar
} }
insertItem.Add(columnInfo); 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) private string GetDbColumnName(string propertyName)

14
Src/Asp.Net/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; }
}
}

1
Src/Asp.Net/SqlSugar/Entities/EntityInfo.cs

@ -19,5 +19,6 @@ namespace SqlSugar
public bool IsDisabledUpdateAll { get; set; } public bool IsDisabledUpdateAll { get; set; }
public List<SugarIndexAttribute> Indexs { get; set; } public List<SugarIndexAttribute> Indexs { get; set; }
public bool IsCreateTableFiledSort { get; set; } public bool IsCreateTableFiledSort { get; set; }
public string Discrimator { get; set; }
} }
} }

1
Src/Asp.Net/SqlSugar/Entities/Mapping/SugarMappingAttribute.cs

@ -14,6 +14,7 @@ namespace SqlSugar
public bool IsDisabledDelete { get; set; } public bool IsDisabledDelete { get; set; }
public bool IsDisabledUpdateAll { get; set; } public bool IsDisabledUpdateAll { get; set; }
public bool IsCreateTableFiledSort { get; set; } public bool IsCreateTableFiledSort { get; set; }
public string Discrimator { get; set; }
public SugarTable(string tableName) { public SugarTable(string tableName) {
this.TableName = tableName; this.TableName = tableName;
} }

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -151,6 +151,7 @@
<Compile Include="Abstract\UpdateProvider\UpdateMethodInfo.cs" /> <Compile Include="Abstract\UpdateProvider\UpdateMethodInfo.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableHelper.cs" /> <Compile Include="Abstract\UpdateProvider\UpdateableHelper.cs" />
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" /> <Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
<Compile Include="Entities\DiscriminatorObject .cs" />
<Compile Include="Entities\PropertyMetadata.cs" /> <Compile Include="Entities\PropertyMetadata.cs" />
<Compile Include="Entities\DbFastestProperties.cs" /> <Compile Include="Entities\DbFastestProperties.cs" />
<Compile Include="Entities\DefaultCustom.cs" /> <Compile Include="Entities\DefaultCustom.cs" />

Loading…
Cancel
Save