Error loading database information to a datagridview via Entity Framework

Asked

Viewed 54 times

0

My project has four classes:

public class Manifestacao
    {
        public long Id { get; set; }
        public string NumeroChamado { get; set; }
        public string DataHoraReg { get; set; }
        public Cliente Cliente { get; set; }
        public Usuario UsuarioRespReg { get; set; }

        public List<Conteudo> Conteudos { get; set; }
    }

public class Conteudo
{
    public long Id { get; set; }
    public string Categoria { get; set; }
    public string SubCategoria { get; set; }
    public string Descricao { get; set; }
    public string DataHoraReg { get; set; }
    public string Status { get; set; }

    public long ManifestacaoId { get; set; }
    public Manifestacao Manifestacao { get; set; }

}

public class Cliente
{
    public long Id { get; set; }
    public string Empresa { get; set; }
    public string Contato { get; set; }
    public string Email { get; set; }
    public string Telefone { get; set; }
    public string Ramal { get; set; }
    public string Celular { get; set; }
}

public class Usuario
{
    public long Id { get; set; }
    public string Apelido { get; set; }
    public string Nome { get; set; }
    public string Categoria { get; set; }
    public string Senha { get; set; }
    public string Status { get; set; }

}

I need to fill out a datagridview with some database information that was persisted through the Entityframework: Content.Category, Content.Subcategory, Content.Description, Content.Status, Client.Company and User.Name

I disabled Lazyloading in the Context class:

public class EFContext : DbContext
{
    public EFContext() : base("Pos_Venda_SAC")
    {
        this.Configuration.LazyLoadingEnabled = false;
        Database.SetInitializer<EFContext>(
        new DropCreateDatabaseIfModelChanges<EFContext>()
        );
    }
    public DbSet<Cliente> Clientes { get; set; }
    public DbSet<Conteudo> Conteudos { get; set; }
    public DbSet<Manifestacao> Manifestacoes { get; set; }
    public DbSet<Usuario> Usuarios { get; set; }
    public DbSet<Destinatario> Destinatarios { get; set; }
}

Based on RU documentation in MSDN I tried to use the class below (Getconteudos()) to read the database, but in datagridview appear the columns with the names of the items of the Content class, but the information that should be associated with the other classes does not appear.

public IList<Conteudo> GetConteudos()
{
    using (var context = new EFContext())
    {
        return context.Conteudos.Include(m => m.Manifestacao.Cliente).Include(u => u.Manifestacao.UsuarioRespReg.Nome).ToList();
    }
}

There was something missing to configure?

  • 1

    How are you doing the Binding from the pro grid list?

  • 1

    You can and should do a Burst that extracts these columns, because datagridview does not work with complex data. Or create a viewmodel e.resolve this..

  • @LINQ dgvListage.Datasource = Getconteudos();

1 answer

1


You will need to properly return the data that must be presented on DataGridView because this control will not solve complex types.

Your code would look like this:

var dados = GetConteudos().Select(x => new 
                           {
                               Status = x.Status,
                               Empresa = Conteudo.Manifestacao.Cliente.Empresa,
                               // e assim por diante
                           }).ToList();

dgvListagem.DataSource = dados;
  • Perfect, @LINQ! Problem solved! Thank you very much! Just a little question... I did a test and rehabilitated Lazyloading by adding the virtual in the statements and commented on the line /this.Configuration.Lazyloadingenabled = false; and yet datagridview continued to display the data correctly. Wouldn’t it be right to stop displaying? I’m starting to use the Entityframework and so the curiosity... rs

  • 1

    No, @Leandro. Lazyloading has nothing to do with displaying the data. It only serves to load related entities on demand.

  • Okay. Thank you!

Browser other questions tagged

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