Picture.Cut.Service/Dolphin.ExamPictureCut.Core/Domains/MySqlConfigureExternalServi...

58 lines
1.8 KiB
C#

using NoFurion;
using SqlSugar;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
namespace Dolphin.ExamPictureCut.Domains;
public class MySqlConfigureExternalServices
{
public static ConfigureExternalServices MySqlExtService = new ConfigureExternalServices
{
EntityService = delegate (PropertyInfo prop, EntityColumnInfo col)
{
if (prop.GetCustomAttribute<NotMappedAttribute>() != null)
{
col.IsIgnore = true;
}
if (!col.IsPrimarykey)
{
if (prop.GetCustomAttribute<RequiredAttribute>() != null)
{
return;
}
if (new NullabilityInfoContext().Create(prop).WriteState == NullabilityState.Nullable || prop.PropertyType == typeof(string))
{
col.IsNullable = true;
}
}
var sugarColumn = prop.GetCustomAttribute<SugarColumn>();
if (sugarColumn != null && sugarColumn.ColumnName.IsNotNullOrEmpty())
{
return;
}
if (prop.DeclaringType.GetCustomAttribute<IgnoreUnderLineAttribute>() == null)
{
col.DbColumnName = UtilMethods.ToUnderLine(col.DbColumnName);
}
},
EntityNameService = delegate (Type t, EntityInfo entity)
{
if (t.GetCustomAttribute<SugarTable>() != null)
{
return;
}
TableAttribute customAttribute = t.GetCustomAttribute<TableAttribute>();
if (customAttribute != null)
{
entity.DbTableName = UtilMethods.ToUnderLine(customAttribute.Name);
}
}
};
}