简体   繁体   English

包含导航属性时,如何阻止 Entity Framework Core 创建“自引用循环”?

[英]How do I stop Entity Framework Core from creating “Self Referencing Loops” when including navigation properties?

Dear fellow programmers!亲爱的程序员!

My issue is that when I include a navigation property, for example: User.Pets, not only the Pets collection gets included in the entity, but every Pet has a reference trough it's navigation property to the User.我的问题是,当我包含导航属性时,例如:User.Pets,不仅 Pets 集合包含在实体中,而且每个 Pet 都通过它的导航属性对用户进行引用。 (LazyLoading is turned off already) (LazyLoading 已经关闭)

This would be fine, since we can use the SelfReferencingLoopHandling.Ignore option in the Json serializer, but in not even so large collections, with lots of navigation properties this becomes extremely slow.这很好,因为我们可以在 Json 序列化程序中使用 SelfReferenceLoopHandling.Ignore 选项,但即使在没有那么大的 collections 中,由于有很多导航属性,这会变得非常慢。

The main cause of this is how the serializer handles references.造成这种情况的主要原因是序列化程序如何处理引用。 There is also a solution for this as well, it's called "PreserveReferencesHandling.Objects", but it messes up the result JSON file.还有一个解决方案,它被称为“PreserveReferencesHandling.Objects”,但它弄乱了结果 JSON 文件。

So, all summed up, it would be nice to find an option, not to create self referencing loops in the first place, and then trying to solve them.因此,总而言之,最好找到一个选项,而不是首先创建自引用循环,然后尝试解决它们。

Thanks everybody!谢谢大家!

It is difficult to understand the problem here, I would say that you can use the tag [JsonIgnore] for the properties that you don´t want to return in the result.这里的问题很难理解,我想说您可以将标签 [JsonIgnore] 用于您不想在结果中返回的属性。

Example: https://www.newtonsoft.com/json/help/html/PropertyJsonIgnore.htm示例: https://www.newtonsoft.com/json/help/html/PropertyJsonIgnore.htm

Types类型


public class Account
{
    public string FullName { get; set; }
    public string EmailAddress { get; set; }

    [JsonIgnore]
    public string PasswordHash { get; set; }
}

Usage:用法:

Account account = new Account
{
    FullName = "Joe User",
    EmailAddress = "joe@example.com",
    PasswordHash = "VHdlZXQgJ1F1aWNrc2lsdmVyJyB0byBASmFtZXNOSw=="
};

string json = JsonConvert.SerializeObject(account);

Console.WriteLine(json);
// {"FullName":"Joe User","EmailAddress":"joe@example.com"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何阻止Entity Framework包含我不希望包含的属性? - How to stop Entity Framework from including properties I don't want it to? 您如何在Entity Framework Core中创建自引用实体? - How do you create a self-referencing entity in Entity Framework Core? 使用InverseProperty时,Entity Framework Core模型中的自引用循环 - Self referencing loop in Entity Framework Core models when using InverseProperty Newtonsoft JsonSerializer使用实体框架核心的自引用循环 - Self referencing loop from Newtonsoft JsonSerializer using Entity Framework Core 如何使用包含导航属性的Entity Framework Core级联进行软删除? - How to make soft delete in cascade with Entity Framework Core including navigation properties? 处置上下文后,如何从实体框架(数据库优先)返回包含导航属性的模型? - How do I return a Model from Entity Framework (Database First) that includes the Navigation Properties after the context is disposed? 如何阻止MVC4 ApiController序列化实体框架导航属性? - How do I prevent an MVC4 ApiController from serializing Entity Framework Navigation Properties? Entity Framework Core 3.0 - 创建一个自引用的多对多关系 - Entity Framework Core 3.0 - Creating a self-referencing many to many relationship 实体框架从导航属性创建新条目 - Entity framework creating new entries from navigation properties 如何使用 Entity Framework Core 中跟踪的 [Owned] 属性获取导航属性 - How to get navigation properties with [Owned] attribute Tracked in Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM