#nullable enable using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; using System.ComponentModel.DataAnnotations; using System.Reflection; namespace Admin.NET.Core { public static class FieldUtil { /// /// 获取标量属性是否必须 /// /// /// /// public static bool IsRequired(IProperty p, string dataType) { if (dataType.StartsWith("System.Nullable")) { return false; } if ("System.String".Equals(dataType)) { return p.PropertyInfo?.IsDefined(typeof(RequiredAttribute), true) ?? false; } return true; } /// /// 获取导航属性是否必须 /// /// /// /// public static bool IsRequired(INavigation p, string dataType) { if (dataType.StartsWith("System.Nullable")) { return false; } return true; } /// /// 获取导航属性是否注解信息 /// /// /// public static string GetComment(this INavigation p) { return ((CommentAttribute?)p?.PropertyInfo?.GetCustomAttribute(typeof(CommentAttribute), true))?.Comment ?? ""; } /// /// 获取类型检称 /// /// /// public static string GetSimpleName(this string fullName) { var args = fullName.Split("."); if (args == null) return ""; return args[^1]; } } }