Load list with Entity

Asked

Viewed 251 times

0

The following. I created a context class inherited from DbContext.

public class SiloContext : DbContext
    {
        public SiloContext()
            : base("inetConn")
        {

        }
        public DbSet<Produto> Produtos { get; set; }
        public DbSet<Balanca> Balancas { get; set; }
    }

Well, then I created another class to load the Product information, based on the Context class.

public class ListaProdutos
{
    private SiloContext contexto = new SiloContext();

    public List<Produto> listaProdutos()
    {
        var prod = // O que colocar aqui? Tentei linq e nada, count == 0 e existe registros no banco.
        //return prod;
    }
}

If the above class works, then in the form at the click of the button, you should click or a Listbox, a Grid and etc...

I changed my model for it and it worked, but Linq takes the credit, so I scored his response.

[Table("Produto")]
    public class Produto
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int idProduto { get; set; }
        public string NMProduto { get; set; }
    }

1 answer

1


You have to access the property Produtos of your context.

public class ListaProdutos
{
    private SiloContext contexto = new SiloContext();

    public List<Produto> listaProdutos()
    {
        return contexto.Produtos.ToList();
    }
}
  • I’ve done it and Count is still 0.

  • Then it is because there is no data @pnet

  • Just answer me this: In my model, I need to report something, like, map the table or just get;set;? If that’s it, something is missing. I just did the model, the get and set.

  • At first you don’t need it. What is the name of the database table? It was generated by EF as well?

  • No, the bank already existed. Do a mapping, I think it’s right, okay? With Fluent or similar. If yes, this mapping I do in another mapping class, am I right? And not in the model.

  • How is the table name of the database?

  • Product, this is the name of the table

  • On top of the class name Produto place an attribute like this [Table("Produto")]. Will need to add namespace System.ComponentModel.DataAnnotations.Schema

Show 3 more comments

Browser other questions tagged

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