title: Remove from collection in Entity Framework 6
body: <blockquote>
    <p>Addition (1): One of the replies said that it ought to work. So I added the complete exception at the end of this post</p>
  </blockquote>
  
  <p>Simplified: I have a sequence of objects, each with a collection of other objects. An example of this is a blog with a collection of posts. MSDN uses this quite often as an example</p>
  
  <p><a href="https://msdn.microsoft.com/nl-nl/data/jj193542.aspx" rel="noreferrer">MSDN Code First to a new DataBase</a></p>
  
  <p>What I see is that if I create a blog, I can add some posts, add the blog to the database and call SaveChanges. The entity framework recognizes that the posts ought to be in a different table with a foreign key to the table of blogs. </p>
  
  <p>This is exactly how one would have created the database if Entity Framework wasn't around.</p>
  
  <p>Getting all Posts from a Blog and adding a Post can be done without any knowledge about the separate Post table and the foreign key to the Blog table. </p>
  
  <p>However, suddenly when I try to remove a post from a blog I get errors about foreign keys.</p>
  
  <blockquote>
    <p>Note: the following is not about efficiency</p>
  </blockquote>
  
  <pre><code>public class Blog
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;Post&gt; Posts { get; set; } 
  }
  
  public class Post
  {
      public int Id { get; set; }
      public string Title { get; set; }
      public string Content { get; set; }
  
      public int BlogId { get; set; }
      public virtual Blog Blog { get; set; } 
  }
  
  public class BloggingContext : DbContext
  {
      public BloggingContext(string nameOrConnectionString)
          : base(nameOrConnectionString){}
  
      public DbSet&lt;Blog&gt; Blogs { get; set; }
      public DbSet&lt;Post&gt; Posts { get; set; }
  }
  </code></pre>
  
  <p>Usage would be as follows:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          const string dbName = "MyTestDb";
          Database.SetInitializer(new DropCreateDatabaseAlways &lt;BloggingContext&gt; ());
          using (var context = new BloggingContext(this.DbName))
          {
              // create a blog:
              var blog = new Blog()
              {
                  Name = "First Blog",
                  Posts = new List&lt;Post&gt;()
                  {
                      new Post() { Title = "My 1st Post", Content = "Hello World!" },
                      new Post() { Title = "My 2nd Post", Content = "All animals are equal but pigs are more equal"},
                      new Post() { Title = "My 3rd Post", Content = "Shall I compare thee to a summer's day" },
                   },
              };
              context.Blogs.Add(blog);
              context.SaveChanges();
  </code></pre>
  
  <p>The nice thing is, that the client doesn't have to know how the database is actually organized. The client doesn't have to know that the Blog and Post are in different tables. For the client a blog has a collection of Posts.</p>
  
  <p>Likewise, if a client asks for a blog and accesses the posts in the blog, Entity Framework knows where to get the posts. The client doesn't have to be aware that the Posts are saved in a different table with a foreign key.</p>
  
  <pre><code>blog = context.Blogs.First();
  var lastPost = blog.Posts.Last();
  </code></pre>
  
  <p>Entity Framework is even thus smart that it doesn't select items that are not needed. If I wouldn't use a Post collection, the Posts wouldn't be retrieved from the database</p>
  
  <p>Therefore I had expected that the following would work:</p>
  
  <pre><code>blog.Posts.Remove(lastPost);
  context.AddOrUpdate(blog);
  context.SaveChanges();
  </code></pre>
  
  <p>I had expected that entity framework would have known that the last post was removed, and thus would order to remove the item from the table of Posts. Yet it doesn't do this. I get an exception that "The relationship could not be changed because one or more of the foreign-key properties is non-nullable. bla bla", meaning that the Post ought to have been removed from the table of Posts.</p>
  
  <blockquote>
    <p>Question: Is it correct that a client doesn't have to know the database model Entity Framework created, except when the data is to be removed?</p>
    
    <p>Addition: Of course I can remove the item from the DbSet, but my question is: If Entity Framework is smart enough the I don't have to add the post to the DbSet, why should I have to remove it?</p>
  </blockquote>
  
  <p><strong>Someone asked for the exception text</strong></p>
  
  <p>System.InvalidOperationException was unhandled</p>
  
  <ul>
  <li>_HResult=-2146233079</li>
  <li><p>_message=The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.</p></li>
  <li><p>HResult=-2146233079</p></li>
  <li>IsTransient=false</li>
  <li>Message=The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.</li>
  <li>Source=EntityFramework</li>
  </ul>
  
  <p>StackTrace:
  at System.Data.Entity.Core.Objects.ObjectContext.PrepareToSaveChanges(SaveOptions options)
  at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
  at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
         at System.Data.Entity.Internal.InternalContext.SaveChanges()
         at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
         at System.Data.Entity.DbContext.SaveChanges()
         at TryInMemoryDbSet.TestDirectDbContext.Test2() in c:\Users\Harald\Documents\Visual Studio 2013\Projects\EntityFramework\TryInMemoryDbSet\TryInMemoryDbSet\TestDirectDbContext.cs:line 75
         at TryInMemoryDbSet.Program.Main(String[] args) in c:\Users\Harald\Documents\Visual Studio 2013\Projects\EntityFramework\TryInMemoryDbSet\TryInMemoryDbSet\Program.cs:line 19
    InnerException: </p>