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

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

View File

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