Popular a List with database data accessed by Entityframework

Asked

Viewed 347 times

0

As popular as mine List, in C#, with the data stored in the database accessed through the Entityframework?

Current code:

 public static IEnumerable<Contato> GetAll()
    {
        return new List<Contato> { //DAqui pa frente nao sei para onde ir . . .//

1 answer

3

Consider that you already have your class that you inherit from Dbcontext configured to work with the Entity Framework... example:

public partial class SeuDBContext : DbContext
{
    ...     
    public DbSet<Contato> Contatos { get; set; }
    ...
}

In addition to your mapped entity, I believe that in the method itself you could do just that:

public IEnumerable<Contato> GetAll()
{
    using(var context = new SeuDBContext())
    { 
        return context.Contatos;
    }
}

Browser other questions tagged

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