我有一个实体为
AppSlider
,它与Action
实体有关系。我想添加一个 AppSlider
并将其保存到 Redis
。
await _redisCacheClient.Db1.AddAsync<AppSlider>(cacheKey, appSlider);
但是当我将其添加到 redis 时,出现错误:
Newtonsoft.Json.JsonSerializationException:'自引用循环 检测到类型为“MyApiName.Domain.AggregatesModel.AppSlider”。 路径“Action.AppSliders”。'
我已经在
Startup.cs
中添加了以下代码:
services.AddControllers()
.AddNewtonsoftJson(x =>
x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
但是,只有保存到Redis Cache时才会出现这个问题。
很晚才回复这个,可能会留给其他人。
我在 docker 容器上本地运行 Redis 以进行本地开发测试, 使用图像 redis:alpine3.16
Visual studio 2019 .Net Framework 4.6.1 Web 应用程序,带有 ADO.NET 数据提供程序 - Devart。
我的数据库上下文设置为启用
LazyLoading
。
禁用
LazyLoading
后,self referencing loop detected
错误不再出现,因为现在未填充导航属性。
如果是这种情况,您可能必须填充导航属性。
这个解决方案可能非常适合我的设置。