Doubt verb GET WebApi

0

I currently have some compositions in my model that relate to other tables in my bank.

When I set up get to bring me all the related data, I had several problems and doubts and when I could solve it, I do not know if it's the right one.

What I did was take the typing of the method so that I could return the JSON. But I saw that I will have many difficulties when I consume this service because it does not come type and in ASP.NET MVC I am waiting for the completed model.

How it was:

public IQueryable GetVerb()
{
    var dados = from d in db.Tabela1
                join a in db.Tabela2 on d.Tabela1_Tabela2Id equals a.Tabela2Id
                select new 
                {
                    nome = d.Nome,
                    cnpj = d.CNPJ,
                    endereco = a.Endereco,
                    cidade = a.Cidade,
                    cep = a.CEP,
                };

    return (dados);             
}

What happens is that I'm not typing with my model Table1 ... Until I've returned, but how do I get WebApi back from models that have a relationship?

I have tried Tabela1.Include() and it did not work, I returned error StackOverflow ...

What could I do to resolve this issue? Could a ViewModel resolve?

EDIT

Table1

public partial class Tabela1
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Tabela1()
    {

    }

    public long Tab1Id { get; set; }
    public Nullable<long> Tab1_EndId { get; set; }
    public Nullable<long> Tab1_PesId { get; set; }
    public Nullable<long> Tab1_RmcId { get; set; }
    public string Tab1Nome { get; set; }
    public string Tab1Apelido { get; set; }
    public string Tab1Cnpj { get; set; }


    public virtual Tabela2 Tabela2 { get; set; }

}

Table2

public partial class Tabela2
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Tabela2()
    {
        this.Tabela1= new HashSet<Tabela1>();
    }

    public long Tab2Id { get; set; }
    public int Tab2_UfeId { get; set; }
    public string Tabela2 { get; set; }
    public string Tab2Bairro { get; set; }
    public string Tab2Cidade { get; set; }
    public int Tab2Cep { get; set; }


    public virtual ICollection<Tabela1> Tabela1 { get; set; }
}
    
asked by anonymous 26.03.2017 / 21:01

1 answer

2

Just remove the reference from the parent class in the child class (or vice versa) and use the include.

This circular reference type is causing Stackoverflow.

    
29.03.2017 / 01:42