Base solution for your next web application
Open Closed

Use of virtual on entity definitions #715


User avatar
0
noah created

In some of the documentation examples, "virtual" keyword is used on scalar entity properties, for example:

<a class="postlink" href="http://aspnetzero.com/Documents/Developing-Step-By-Step#creating-person-entity">http://aspnetzero.com/Documents/Develop ... son-entity</a>

I'm aware of using virtual to lazy load related entities from another table, but not clear on why "virtual" is used with string properties in these examples? Thanks!


5 Answer(s)
  • User Avatar
    0
    paddyfink created

    it's a requirement for EF to be able to create poco proxies

    <a class="postlink" href="http://stackoverflow.com/questions/5597760/what-effects-can-the-virtual-keyword-have-in-entity-framework-4-1-poco-code-fi">http://stackoverflow.com/questions/5597 ... co-code-fi</a>

  • User Avatar
    0
    noah created

    Thanks. Perhaps then the question should be: when defining entity classes under ABP, is there a case where a member should not be marked as virtual?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Actually, EF does not require virtual for primitive types (those are not lazy loadable). I used to do it because NHibernate requires :). Also, it will be possible for derived entities to override it.

  • User Avatar
    0
    noah created

    Thanks. So you would say that if using EF, only use "virtual" on navigation properties and not for primitive types?

  • User Avatar
    0
    hikalkan created
    Support Team

    Yes.