title: What is a proper way of writing entity POCO classes in Entity Framework Core?
body: <p>EF Core has a "code first mentality" by default, i.e. it is supposed to be used in a code-first manner, and even though database-first approach is supported, it is described as nothing more than reverse-engineering the existing database and creating code-first representation of it. What I mean is, the model (POCO classes) created in code "by hand" (code-first), and generated from the database (by Scaffold-DbContext command), should be identical.</p>
  
  <p>Surprisingly, official EF Core docs demonstrate significant differences. Here is an example of creating the model in code: <a href="https://ef.readthedocs.io/en/latest/platforms/aspnetcore/new-db.html" rel="noreferrer">https://ef.readthedocs.io/en/latest/platforms/aspnetcore/new-db.html</a> And here is the example of reverse-engineering it from existing database: <a href="https://ef.readthedocs.io/en/latest/platforms/aspnetcore/existing-db.html" rel="noreferrer">https://ef.readthedocs.io/en/latest/platforms/aspnetcore/existing-db.html</a></p>
  
  <p>This is the entity class in first case:</p>
  
  <pre><code>public class Blog
  {
      public int BlogId { get; set; }
      public string Url { get; set; }
  
      public List&lt;Post&gt; Posts { get; set; }
  }
  
  public class Post
  {
      public int PostId { get; set; }
      public string Title { get; set; }
      public string Content { get; set; }
  
      public int BlogId { get; set; }
      public Blog Blog { get; set; }
  }
  </code></pre>
  
  <p>and this is the entity class in second case:</p>
  
  <pre><code>public partial class Blog
  {
      public Blog()
      {
          Post = new HashSet&lt;Post&gt;();
      }
  
      public int BlogId { get; set; }
      public string Url { get; set; }
  
      public virtual ICollection&lt;Post&gt; Post { get; set; }
  }
  </code></pre>
  
  <p>The first example is a very simple, quite obvious POCO class. It is shown everywhere in the documentation (except for the examples generated from database). The second example though, has some additions:</p>
  
  <ul>
  <li>Class is declared <strong>partial</strong> (even though there's nowhere to be seen another <strong>partial</strong> definition of it).</li>
  <li>Navigation property is of type <strong>ICollection&lt; T ></strong>, instead of just List&lt; T >.</li>
  <li>Navigation property is initialized to <strong>new HashSet&lt; T >()</strong> in the constructor. There is no such initialization in code-first example.</li>
  <li>Navigation property is declared <strong>virtual</strong>.</li>
  <li>DbSet members in a generated context class are also <strong>virtual</strong>.</li>
  </ul>
  
  <p>I've tried scaffolding the model from database (latest tooling as of this writing) and it generates entities exactly as shown, so this is not an outdated documentation issue. So the official tooling generates different code, and the official documentation suggests writing different (trivial) code - without partial class, virtual members, construction initialization, etc.</p>
  
  <p>My question is, trying to build the model in code, how should I write my code? I like using ICollection instead of List because it is more generic, but other than that, I'm not sure whether I need to follow docs, or MS tools? Do I need to declare them as virtual? Do I need to initialize them in a constructor? etc...</p>
  
  <p>I know from the old EF times that virtual navigation properties allow lazy loading, but it is not even supported (yet) in EF Core, and I don't know of any other uses. Maybe it affects performance? Maybe tools try to generate future-proof code, so that when lazy-loading will be implemented, the POCO classes and context will be able to support it? If so, can I ditch them as I don't need lazy loading (all data querying is encapsulated in a repo)?</p>
  
  <p>Shortly, please help me understand why is the difference, and which style should I use when building the model in code?</p>