重写sqlsugar判断列长度的方法

This commit is contained in:
falcon 2023-03-02 10:37:02 +08:00
parent eb66061fa2
commit 128f5f6025

View File

@ -1,4 +1,5 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
@ -9,22 +10,19 @@ namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
/// <summary>
/// 设置长度规则
/// </summary>
public class SetupLengthColumnServices : IEntityColumnServices
public class SetupLengthColumnServices:IEntityColumnServices
{
/// <inheritdoc/>
public void SetupColumn(PropertyInfo p, EntityColumnInfo c) {
var len = new List<int>();
if (p.TryGetAttribute<StringLengthAttribute>(out var sl)) {
public void SetupColumn(PropertyInfo p,EntityColumnInfo c) {
var len = new List<int> { };
if(p.TryGetAttribute<StringLengthAttribute>(out var sl)) {
len.Add(sl.MaximumLength);
}
if (p.TryGetAttribute<MaxLengthAttribute>(out var la)) {
if(p.TryGetAttribute<MaxLengthAttribute>(out var la)) {
len.Add(la.Length);
}
if (p.TryGetAttribute<SugarColumn>(out var sc)) {
len.Add(sc.Length);
}
if (len.Any()) {
c.Length = len.Max();
if(len.Any()) {
c.Length=len.Reduce(c.Length,(m,i) => Math.Max(m,i));
}
}
}