简体   繁体   English

当在DbSet中使用Include时,C#实体框架返回损坏的JSon

[英]C# Entity Framework returning broken JSon when using Include in DbSet

I'm trying to make a simple one-to-many relationship in C# using Entity Framework Core, the problem resembles a circular reference problem but no exception is being thrown. 我正在尝试使用Entity Framework Core在C#中建立简单的一对多关系,该问题类似于循环引用问题,但没有引发异常。

My model classes are: 我的模型类是:

[Table("Restaurantes")]
public class Restaurante
{
    public int Id { get; set; }
    public string Descricao { get; set; }
    public List<Prato> Pratos { get; set; }
}

[Table("Pratos")]
public class Prato
{
    public int Id { get; set; }
    public string Descricao { get; set; }
    public decimal Preco { get; set; }

    [ForeignKey("RestauranteForeignKey")]
    public Restaurante Restaurante { get; set; }

}
// One Prato (Dish) belongs to One Restaurant and one Restaurant can have many Dishes (Pratos)

I'm returning the Pratos from DB and I want that it returns also with their respective Restaurant: 我要从DB归还Pratos,我希望它也与各自的Restaurant一​​起归还:

context.Pratos.Include(r => r.Restaurante).ToList();

The context definition: 上下文定义:

public class Contexto : DbContext
{
    public DbSet<Restaurante> Restaurantes { get; set; }
    public DbSet<Prato> Pratos { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Prato>()
            .HasOne(p => p.Restaurante)
            .WithMany(r => r.Pratos)
            .OnDelete(DeleteBehavior.Cascade);
    }
}

But on the browser, the JSON returned is something like: 但是在浏览器上,返回的JSON类似于:

[ {"id":1,"descricao":"prato 1","preco":1.50,"restaurante":{"id":1,"descricao":"fulano","pratos":[

It returns no more than that. 它返回的仅此而已。 I also tried the documentation but didn't helped in terms of this problem. 我也尝试过文档,但是在这个问题上没有帮助。 Thanks in advance. 提前致谢。

If you are using Json.net you probably need to change the DefaultSettings to handle the serialization 如果您使用的是Json.net ,则可能需要更改DefaultSettings来处理序列化

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.All,
    ReferenceLoopHandling = ReferenceLoopHandling.Serialize
};

Or depending on your particular requirements 或根据您的特定要求

PreserveReferencesHandling

  • None : Do not preserve references when serializing types. :序列化类型时不保留引用。
  • Objects : Preserve references when serializing into a JSON object structure. 对象 :序列化为JSON对象结构时保留引用。
  • Arrays : Preserve references when serializing into a JSON array structure. 数组 :序列化为JSON数组结构时保留引用。
  • All : Preserve references when serializing. 全部 :序列化时保留引用。

ReferenceLoopHandling

  • Error : Throw a JsonSerializationException when a loop is encountered. 错误 :遇到循环时引发JsonSerializationException。
  • Ignore : Ignore loop references and do not serialize. 忽略 :忽略循环引用,不进行序列化。
  • Serialize :Serialize loop references. 序列化 :序列化循环引用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM