title: How to add a parent record with its children records in EF Core
body: <p>Example 1 below used to work but it is not working in EF Core. <strong>Question</strong>: How to make example 2 below work?</p>
  
  <p><strong>Example 1</strong>:</p>
  
  <pre><code>child c1 = new child();
  child c2 = new child();
  
  parent p=new parent();
  p.child.Add(c1);
  p.child.Add(c2);
  
  using (var db = new DbContext())
  {
     db.parent.Add(p);
     db.SaveChanges();
  }
  </code></pre>
  
  <p>Entity Parent P has children C1, and C2 in a one-to-one relationship. Trying to insert a parent and its children records. But in the following code <code>VS2017</code>'s editor's <code>intellisense</code> does not recognize <code>.child</code> at the line <code>cust.child.Add(c1);</code>. Maybe, EF Core has something better for inserting parent/child records. I'm using ASP.NET <code>MVC Core 1.1.1</code> and EF Core 1.1.1.</p>
  
  <p><strong>Example 2</strong>:</p>
  
  <pre><code>...
  Parent p = new Parent { Name = "some name" , FY = SelectedYear, ... };
  
  Child c1 = new Child { ItemName = "Abc"};
  Child c2 = new Child { ItemName = "Rst"};
  
  p.child.Add(c1);
  p..child.Add(c2);
  
  _context.Add(p);
  _context.SaveChanges();
  </code></pre>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>Per a request from <code>@GlennSills</code>, following is an example of well known Blogging Db (taken from <a href="https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db" rel="noreferrer">this</a> ASP.NET tutorial):</p>
  
  <pre><code>public class BloggingContext : DbContext
  {
      public BloggingContext(DbContextOptions&lt;BloggingContext&gt; options)
          : base(options)
      { }
  
      public DbSet&lt;Blog&gt; Blogs { get; set; }
      public DbSet&lt;Post&gt; Posts { get; set; }
  }
  
  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>In the <code>Create(...)</code> method below, at line <code>blog.Posts.Add(c);</code> I get the error: <code>Object reference not set to an instance of an object.</code></p>
  
  <pre><code>public class BlogsController : Controller
  {
      private readonly BloggingContext _context;
  
      public BlogsController(BloggingContext context)
      {
          _context = context;    
      }
  ....
  ....
  // POST: Blogs/Create
  // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
  // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  [HttpPost]
  [ValidateAntiForgeryToken]
  public async Task&lt;IActionResult&gt; Create([Bind("BlogId,Url")] Blog blog)
  {
      if (ModelState.IsValid)
      {
          Post c = new Post();
          blog.Posts.Add(c);
  
          _context.Add(blog);
          await _context.SaveChangesAsync();
          return RedirectToAction("Index");
      }
      return View(blog);
  }
  }
  </code></pre>