title: EF Core defeats the purpose of constructor with null checking
body: <p>I have designed my models so that it should <strong>not</strong> be created with a <code>null</code> parameter. For example, if I want to enforce that every <code>Post</code> should have a corresponding <code>Blog</code>, then my models will look like:</p>
  
  <pre><code>public class Post
  {
      private Post() { }
      public Post(Blog blog)
      {
          Blog = blog ?? throw new ArgumentNullException(nameof(blog));
      }
      public int PostId { get; private set; }
      public Blog Blog { get; private set; }
  }
  public class Blog
  {
      public int BlogId { get; private set; }
  }
  </code></pre>
  
  <p>This way, it will throw an <code>Exception</code> if <code>blog</code> is <code>null</code>.</p>
  
  <p>But I use EF Core and it fails this test.</p>
  
  <pre><code>public class Tests
  {
      [Fact]
      public void Test()
      {
          using (var ctx = new Context())
          {
              ctx.Database.EnsureDeleted();
              ctx.Database.EnsureCreated();
              ctx.Add(new Post(new Blog()));
              ctx.SaveChanges();
          }
          using (var ctx = new Context())
          {
              var post = ctx.Post.First();
              Assert.NotNull(post.Blog); //fail
          }
      }
  }
  public class Context : DbContext
  {
      public DbSet&lt;Post&gt; Post { get; set; }
      public DbSet&lt;Blog&gt; Blog { get; set; }
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Post&gt;(b =&gt; b.HasOne(p =&gt; p.Blog));
      }
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =&gt; optionsBuilder.UseSqlite("datasource=db.sqlite");
  }
  </code></pre>
  
  <p>I know that it's because EF Core calls the private constructor without parameters and that I need to load (i.e. Eager, Explicit, or Lazy) the <code>Post.Blog</code> navigational property.</p>
  
  <p>But what I want to know is if my approach on the design of my EF models are incorrect since EF Core defeats the purpose of constructor with null checking?</p>
  
  <p>EDIT: With C#8's nullable reference types, EF Core may head into the direction where it can set navigational properties using a constructor. See EF Core issue: <a href="https://github.com/aspnet/EntityFrameworkCore/issues/10347" rel="nofollow noreferrer">Support C# nullable references</a></p>