Doubt Verb GET Webapi

Asked

Viewed 86 times

0

Currently I have some compositions in my model that make relationship with other tables of my bank.

When I went to assemble the get to bring me all the related data, I had several problems and doubts and when I managed to solve, I don’t know if it is 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 I do not see type and on Asp.net mvc I am waiting for the completed model.

As it turned out:

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);             
}

The thing is, I’m not dating my Tabela1 model... Even returned, but how do I make the Webapi return me also models that have relationship?

I’ve tried to Tabela1.Include() and did not work, returned me error Stackoverflow...

What could I do to resolve this issue? A Viewmodel could solve?

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; }
}
  • 1

    1. What is the intention of Join if nowhere are used the data of the second table? 2. What’s wrong with the Include? It was not to give Stackoverflow, this must be another problem, give more details. 3. Why is returning one IQueryable?

  • @jbueno, in order: The data is used, but in the MVC, in the view. So, I don’t know what happens when I call the include... The error is generated and does not return anything to me. Iqueryable because it was already so when I created the controller in the web api...

  • If you do not specify in select the data from the second table, they will not exist within dados. What is the exact error you get when using Include?

  • Returns me Stack Overflow error...

  • Dude, just saying that won’t help. You have to have stack trace, the message should also have more details. If gave Stackoverflow there is some reason, but without more details is impossible to know.

  • I wanted to popular the Tabela1 model because in MVC I expect this populated model to show the data in the view

  • Got it! As soon as I get home I take the Stack trace and put the question... Then I mark you, it might be?

  • @jbueno what returns me in stack trace is: System.StackOverflowException was unhandled&#xA;Message: An unhandled exception of type 'System.StackOverflowException' occurred in System.Web.dll&#xA; and informing that I may be making an infinite loop...

  • So you have circular dependencies... Post the models.

  • My models are created by Entity in database first.. There are some problems?

  • @jbueno, models in the question!

  • 1

    You have father reference in son and son in father. That’s what’s causing Stackoverflow

  • @jbueno, it worked! Look, thank you very much!!!!! Could you put as an answer? Even if it is simple!

  • 1

    Okay. I’ll put it on, then I’ll get better. I’m on the phone now.

Show 9 more comments

1 answer

2


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

This type of circular reference is causing Stackoverflow.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.