Error loading database information into a datagridview through the Entity Framework

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 populate a datagridview with some database information that has been persisted through 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 the EF documentation at #

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

Have you set something up?

    
asked by anonymous 30.08.2017 / 20:16

1 answer

1

You will need to properly return the data that should be displayed in DataGridView because this control will not solve complex types.

Your code looks like this:

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

dgvListagem.DataSource = dados;
    
30.08.2017 / 20:54