Access FK attribute in Entity Framewokr

Asked

Viewed 40 times

0

I have the class Cliente who owns a IList<Produto> and the class of produto has a ClienteId (which is FK) and an attribute Cliente Cliente { get; set; }. How to access the attribute Nome of Cliente as a product?

public class Cliente 
{
  ....
  public string Nome {get;set;}

  public IList<Produto> Produtos {get;set;}
}

public class Produto
{
  ....
  public int ClienteId {get;set;}

  public Cliente Cliente{get;set;}
}

NOTE: all fields are mapped, and are bringing data.

  • I didn’t understand if you have something more or your doubt is simpler than I think. But if all the data is being filled in, just use produto.Cliente.Name, being produto the instance of the class Produto. There is also a typo in your classes, is referencing the class Produto but the class name is Produtos.

  • I wanted to access the property Nome of Cliente in Produto,&#I tried that way Produto.cliente.Nome and ProdutoClienteNome, and not for sure.

  • Tried to access properties via debug? Is there an error trying to access? Intellisense gives the name of the properties?

  • 2

    @Vinicius, try to change the Client property as virtual, to allow Entity to do Lazy loading.

  • @Juniorpirio worked out to change to virtual

No answers

Browser other questions tagged

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