Entity mapping with . Net Core 2

Asked

Viewed 51 times

1

have the following entities:

 public class Transacao : IEntity
    {
        [Key]
        public int Id { get; private set; }

        public int LojaId { get; set; }
        public virtual Loja Loja { get; set; }

        public int AdquirenteId { get; set; }
        public virtual Adquirente Adquirente { get; set; }

        public DateTime Data { get; set; }
        public Guid Numero { get; set; }
        public int NumeroPedido { get; set; }
        public Decimal Valor { get; set; }
    }

public class Adquirente : IEntity
    {
        [Key]
        public int Id { get; private set; }

        [Required]
        [StringLength(50)]
        [Display(Name = "Nome")]
        public string Nome { get; set; }

        public int AdquirenteId { get; set; }

        public List<LojaAdquirente> LojaAdquirentes { get; set; }
    }

public class Loja : IEntity
    {
        [Key]
        public int Id { get; private set; }

        [Required]
        [StringLength(18)]
        [Display(Name = "CNPJ")]
        public string CNPJ { get; set; }

        [Required]
        [StringLength(50)]
        [Display(Name = "Nome")]
        public string Nome { get; set; }

        [Required]
        [StringLength(50)]
        [Display(Name = "Endereço")]
        public string Endereco { get; set; }

        [Required]
        [Display(Name = "Possui Antifraude Habilitado")]
        public bool IsAntiFraudeEnabled { get; set; }

        public Guid Token { get; set; }

        public int LojaId { get; set; }

        public List<LojaAdquirente> LojaAdquirentes { get; set; }
    }

When I will retrieve the records from the database (_context.Transacao.Where(x => x.Loja.Token.Equals(tokenLoja)); the entities Store and Acquirer are coming null. What I need to do to carry these entities together with the parent entity?

  • 3

    If you can use Include (which is the early loading), you can use the Lazyload package for the lazy loading or even the Ilazyloader interface ... Quick action use Include. _context.Transacao.Include(i=>i.LojaAdquirentes).Where(x => x.Loja.Token.Equals(tokenLoja))

  • 1

    Thank you Virgil! It worked! :)

No answers

Browser other questions tagged

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