Doubt with virtual properties

Asked

Viewed 49 times

0

I have two tables: A and B

On the table A have:

Id
Nome

On the table B have:

Id
Nome
IdA(FK) -> de A

In the application would be that?

public class A
{
  public int Id { get; set; }
  public string Nome { get; set; }
  public ICollection<B> Bs { get; set; }
}

and in B I would do

public class B
{
  public int Id { get; set; }
  public string Nome { get; set; }
  public int? IdA { get; set; }
  public virtual A A { get; set; }
}

Is that or opposite? Just to understand about virtual properties

1 answer

3

Marking a property as virtual indicates that it can be overwritten by child classes. Some frameworks like EntityFramework use the virtual properties to create Proxies, allowing the implementation of lazy loading.

Also in the case of Entityframework, the Navigation Property need to be marked as virtual if you want to enable the Lazy Loading.

In answer to that question there are more details about virtual properties and Lazy loading.

Browser other questions tagged

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