ADO.NET Navigation Properties

I'm having a little problem using the navigation and inheritance properties on ADO.NET.

This is my data model:

ADO.NET Data Model

First, some dictionary:

Category = Category
Formulario =
Campo Form =
Imagem Field =
Paragrafo Image = Paragraph
Escolha = Select
Texto = Text
Resposta = Answer

So, I'm trying to create a custom property in a form that returns its number of responses.

Normal approach (I think):

public partial class Formulario
{
    public int Respostas
    {
        get
        {
            List<Item> itens = this.Itens.ToList();

            IEnumerable<Campo> campos = from m in itens where m as Campo != null select (Campo)m;
            int soma = campos.Sum(m => m.Respostas.Count);

            return soma;
        }
    }
}

But that will not work. The list itensreturns 0 items. But when I do, as shown below, it returns 4 elements that it should:

public partial class Formulario
{
    public int Respostas
    {
        get
        {
            FormulariosDB db = new FormulariosDB();

            List<Item> itens = db.Items.Where(m => m.Formulario.Id == this.Id).ToList();

            IEnumerable<Campo> campos = from m in itens where m as Campo != null select (Campo)m;
            int soma = campos.Sum(m => m.Respostas.Count);

            return soma;
        }
    }
}

This only works when I run the entire data model. Does anyone know why?

PS: .toList(), Linq, Linq2Entities

+3
1

, Entity Framework 1 , Entity Framework 4.

Itens. , , , Formulario .

, :

if(!Itens.IsLoaded)
    Itens.Load();

List<Item> itens = Itens.ToList();
+2

Source: https://habr.com/ru/post/1775644/


All Articles