BUG:实体中如果对字段设置了JsonIgnore就保存不出数据到种子数据里

This commit is contained in:
yzp 2024-12-18 11:55:55 +08:00
parent 098dd4ad39
commit 2aa6a5bf67

View File

@ -406,34 +406,35 @@ public class SysDatabaseService : IDynamicApiController, ITransient
} }
} }
// 检查有没有 System.Text.Json.Serialization.JsonIgnore 的属性 // 检查有没有 System.Text.Json.Serialization.JsonIgnore 或 Newtonsoft.Json.JsonIgnore 的属性
// var jsonIgnoreProperties = entityType.GetProperties().Where(p => (p.GetAttribute<System.Text.Json.Serialization.JsonIgnoreAttribute>() != null || // 如果 JsonIgnore 和 SugarColumn 都存在那么后成序更化时就生成这了这些字段就需要在这里另外补充以处理用户表SysUser中的Password为例
// p.GetAttribute<JsonIgnoreAttribute>() != null) && p.GetAttribute<SugarColumn>() != null).ToList(); var jsonIgnoreProperties = entityType.GetProperties().Where(p => (p.GetAttribute<System.Text.Json.Serialization.JsonIgnoreAttribute>() != null ||
// var jsonIgnoreInfo = new List<List<JsonIgnoredPropertyData>>(); p.GetAttribute<Newtonsoft.Json.JsonIgnoreAttribute>() != null) && p.GetAttribute<SugarColumn>() != null).ToList();
// if (jsonIgnoreProperties.Count > 0) var jsonIgnoreInfo = new List<List<JsonIgnoredPropertyData>>();
// { if (jsonIgnoreProperties.Count > 0)
// int recordIndex = 0; {
// foreach (var r in (IEnumerable)records) int recordIndex = 0;
// { foreach (var r in (IEnumerable)records)
// List<JsonIgnoredPropertyData> record = new(); {
// foreach (var item in jsonIgnoreProperties) List<JsonIgnoredPropertyData> record = new();
// { foreach (var item in jsonIgnoreProperties)
// object v = item.GetValue(r); {
// string strValue = "null"; object v = item.GetValue(r);
// if (v != null) string strValue = "null";
// { if (v != null)
// strValue = v.ToString(); {
// if (v.GetType() == typeof(string)) strValue = v.ToString();
// strValue = "\"" + strValue + "\""; if (v.GetType() == typeof(string))
// else if (v.GetType() == typeof(DateTime)) strValue = "\"" + strValue + "\"";
// strValue = "DateTime.Parse(\"" + ((DateTime)v).ToString("yyyy-MM-dd HH:mm:ss") + "\")"; else if (v.GetType() == typeof(DateTime))
// } strValue = "DateTime.Parse(\"" + ((DateTime)v).ToString("yyyy-MM-dd HH:mm:ss") + "\")";
// record.Add(new JsonIgnoredPropertyData { RecordIndex = recordIndex, Name = item.Name, Value = strValue }); }
// } record.Add(new JsonIgnoredPropertyData { RecordIndex = recordIndex, Name = item.Name, Value = strValue });
// recordIndex++; }
// jsonIgnoreInfo.Add(record); recordIndex++;
// } jsonIgnoreInfo.Add(record);
// } }
}
// 获取所有字段信息 // 获取所有字段信息
var propertyList = entityType.GetProperties().Where(x => false == (x.GetCustomAttribute<SugarColumn>()?.IsIgnore ?? false)).ToList(); var propertyList = entityType.GetProperties().Where(x => false == (x.GetCustomAttribute<SugarColumn>()?.IsIgnore ?? false)).ToList();
@ -457,7 +458,7 @@ public class SysDatabaseService : IDynamicApiController, ITransient
input.SeedDataName, input.SeedDataName,
input.ConfigId, input.ConfigId,
tableInfo.Description, tableInfo.Description,
JsonIgnoreInfo = new List<List<JsonIgnoredPropertyData>>(), JsonIgnoreInfo = jsonIgnoreInfo,
RecordList = recordList RecordList = recordList
}; };
var tResult = await _viewEngine.RunCompileAsync(tContent, data, builderAction: builder => var tResult = await _viewEngine.RunCompileAsync(tContent, data, builderAction: builder =>