Recursion in the entity-framework

1

I'm starting in C # and I'm having some difficulties with the Entity-Framework. I have the following scenario:

public InstituicaoMap()
{
    // Primary Key
    HasKey(t => t.IdInstituicao);

    // Properties
    Property(t => t.Nome)
    .IsRequired()
    .HasMaxLength(255);
}

public CampanhaMap()
{
    // Primary Key
    this.HasKey(t => t.IdCampanha);

    // Properties
    this.Property(t => t.Texto)
        .IsRequired()
        .HasMaxLength(160);

    this.Property(t => t.DataEdicao)
        .IsFixedLength()
        .HasMaxLength(8)
        .IsRowVersion();

    // Relationships
    this.HasRequired(t => t.Instituicao)
        .WithMany(t => t.Campanhas)
        .HasForeignKey(d => d.IdInstituicao);
}

I'm implementing a campaign CRUD, and I'm currently running the " IEnumerable<Campanha> List() " method (which lists all registered campaigns).

Apparently this is working. For each campaign returned in the "list" there is an Institution object inside. Until then. The problem is that within the Institution there is a list of campaigns, that there are institutions, that there is another list of campaigns ... and so on.

1 - I'm worried about memory consumption and / or some problem that this recursion might cause.

2 - I'm sending this return to View via JSON (since grid pagination will be done with AJAX / JSON).

return new ContentResult
{
    Content = JsonConvert.SerializeObject(v),
    ContentType = "application/json"
};

On this return, I'm getting an "infinite looping" error from JsonConvert. In fact, because the campaign object is recursive.

What can I do?

    
asked by anonymous 03.05.2016 / 18:09

2 answers

5

You can use the annotation in your list, or map to ignore, in your configuration. The first mode is simpler, just do this:

[JsonIgnore]
public virtual Instituicao Instituicao{get;set;}

In the second way, it would look like this CampanhaMap :

public CampanhaMap()
{
    // Primary Key
    this.HasKey(t => t.IdCampanha);

    // Properties
    this.Property(t => t.Texto)
        .IsRequired()
        .HasMaxLength(160);

    this.Property(t => t.DataEdicao)
        .IsFixedLength()
        .HasMaxLength(8)
        .IsRowVersion();

    // Relationships
    this.HasRequired(t => t.Instituicao)
        .WithMany(t => t.Campanhas)
        .HasForeignKey(d => d.IdInstituicao);

    this.Ignore(t => t.Instituicao);

}

Or, if not that, you can disable% EF_com% in its context. Something like this:

  public YouContext(): base("name=SchoolDBEntities")
    {
        this.Configuration.LazyLoadingEnabled = false;
    }
    
03.05.2016 / 18:17
-1

Another way would be to create your own class for your needs.

Using AutoMapper in your project and not using EF classes for display and return.

There is a story of Eduardo Pires talking about SOLID.

I advise you not to use Lazy Loading because it does separate queries at the base. The Vandage is that when you need the data it's just calling the property that the query is done automatically.

Tip: Use Eager Loading where you tell EF what you need to load.

I killed myself a lot with EF6 is not easy is very confusing and does not have many subjects on. I recommend seeing these sites ...

link

link

    
02.06.2017 / 22:27