Query with related data in the Entity Framework

Asked

Viewed 2,419 times

2

I am trying to bring data of the materials with the data of your relationships with the Unit and TD tables, but this code below does not return anything, if I do without Include() it returns the data but with the relationship does not bring any results. In the Material class I have the navigation properties for Unit and TD.

    public IEnumerable<Material> Listar(String Nome)
    {
        IEnumerable<Material> materiais = (from x in db.Material
                                           .Include("Unidade")
                                           .Include("TD")
                                           .Where(x => x.Nome.Contains(Nome)) select x).ToList();                               
        return materiais;
    } 

SQL code generated by Entity:

SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[CodTron] AS [CodTron], 
[Extent1].[RefTron] AS [RefTron], 
[Extent1].[Nome] AS [Nome], 
[Extent1].[UnidadeID] AS [UnidadeID], 
[Extent1].[TDID] AS [TDID], 
[Extent2].[ID] AS [ID1], 
[Extent2].[CodTron] AS [CodTron1], 
[Extent2].[Referencia] AS [Referencia], 
[Extent2].[Descricao] AS [Descricao], 
[Extent2].[Simbolo] AS [Simbolo], 
[Extent3].[ID] AS [ID2], 
[Extent3].[Numero] AS [Numero], 
[Extent3].[Descricao] AS [Descricao1], 
[Extent3].[RefTron] AS [RefTron1]
FROM   [dbo].[Material] AS [Extent1]
INNER JOIN [dbo].[Unidade] AS [Extent2] ON [Extent1].[UnidadeID] = [Extent2].[ID]
INNER JOIN [dbo].[TD] AS [Extent3] ON [Extent1].[TDID] = [Extent3].[ID]
WHERE [Extent1].[Nome] LIKE @p__linq__0 ESCAPE N'~'
  • What data is already included in the database? Which String you are using for research?

  • I have over 600 items in the database in the Material table, I am searching a material I am sure has in the database, and as I said, when I take out the Include it brings the result correctly but without the data from TD and Drive.

  • Try to take the query generated by doing the following: var stringTeste = (from x in db.Material.Include("Unidade").Include("TD").Where(x => x.Nome.Contains(Nome))).ToString(). Put the String generated in SQL Server Management Studio and see what happens.

  • the error string, Must declare scalar variable @p__linq_0, see, updated my question and posted the generated SQL code.

  • Even if I replace the parameter with a material name SQL Server does not return anything to me.

2 answers

3


Diego and Gypsy Morrison, I found the error: it was an error in the table of Unidade in the database. By default the Entity Framework will do the Inner Joins for the browsing properties, in my case the UnidadeID and the TDID, but I need the association to be made by another table field, the CodTron, that is, the correct comparison is Unidade.MaterialID = Unidade.CodTron and in the TD table the purchase would be Materia.TDID = TD.CodTron, this happens in my case why the data of these two tables comes from another system and are imported via Integration Services, and associations should be made by codes that are in the other system.

Because of this error he ends up making no reference to data in table of Unidade so don’t bring the data. After @Cigano spoke to put the code generated by Entity in Management Studio I saw that even replacing the parameter with the name of the material it did not bring the data, so I started investigating the database more and found the error.

Thank you.

  • Could you mark this answer as accepted, better describing this error, for the benefit of this community? I thank you already.

  • I can only schedule my own answer within 2 days, if you feel it appropriate please tick.

  • @user36700112 Okay, thanks.

2

Try this:

IEnumerable<Material> materiais = db.Material
                                       .Include(u=>u.Unidade)
                                       .Include(t=>t.TD)
                                       .Where(x => x.Nome.Contains(Nome)).ToList(); 

Don’t forget to import the reference:

using System.Data.Entity;
  • Diego, continues the same way, does not return any information.

  • @user3670112, try now, no need for from. Add to question the Material, Unit, and TD classes to illustrate your environment.

  • Still the problem Diego.

Browser other questions tagged

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