title: Delete parent with children in one to many relationship
body: <p>I have a .NET4.0 application with Entity Framework 5.0 e Sql Server CE 4.0.</p>
  
  <p>I have two entities with a one to many (parent/child) relationship. I've configured it to cascade delete on parent removal, but for some reason it doesn't seem to work.</p>
  
  <p>Here is a simplified version of my entities:</p>
  
  <pre><code>    public class Account
      {
          public int AccountKey { get; set; }
          public string Name { get; set; }
  
          public ICollection&lt;User&gt; Users { get; set; }
      }
  
      internal class AccountMap : EntityTypeConfiguration&lt;Account&gt;
      {
          public AccountMap()
          {
              this.HasKey(e =&gt; e.AccountKey);
              this.Property(e =&gt; e.AccountKey).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
              this.Property(e =&gt; e.Name).IsRequired();
          }
      }
  
  
      public class User
      {
          public int UserKey { get; set; }
          public string Name { get; set; }
  
          public Account Account { get; set; }
          public int AccountKey { get; set; }
      }
  
      internal class UserMap : EntityTypeConfiguration&lt;User&gt;
      {
          public UserMap()
          {
              this.HasKey(e =&gt; e.UserKey);
              this.Property(e =&gt; e.UserKey).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
              this.Property(e =&gt; e.Name).IsRequired();
  
  
              this.HasRequired(e =&gt; e.Account)
                  .WithMany(e =&gt; e.Users)
                  .HasForeignKey(e =&gt; e.AccountKey);
          }
      }
  
      public class TestContext : DbContext
      {
          public TestContext()
          {
              this.Configuration.LazyLoadingEnabled = false;
          }
  
          public DbSet&lt;User&gt; Users { get; set; }
          public DbSet&lt;Account&gt; Accounts { get; set; }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;(); modelBuilder.Conventions.Remove&lt;StoreGeneratedIdentityKeyConvention&gt;();
              modelBuilder.LoadConfigurations();
          }
  
      }
  </code></pre>
  
  <p>The connection string:</p>
  
  <pre><code>  &lt;connectionStrings&gt;
      &lt;add name="TestContext" connectionString="Data Source=|DataDirectory|\TestDb.sdf;" providerName="System.Data.SqlServerCe.4.0" /&gt;
    &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>And a simplified version of my app's workflow:</p>
  
  <pre><code>static void Main(string[] args)
  {
      try
      {
          Database.SetInitializer(new DropCreateDatabaseAlways&lt;TestContext&gt;());
          using (var context = new TestContext())
              context.Database.Initialize(false);
  
          Account account = null;
          using (var context = new TestContext())
          {
              var account1 = new Account() { Name = "Account1^" };
              var user1 = new User() { Name = "User1", Account = account1 };
  
              context.Accounts.Add(account1);
              context.Users.Add(user1);
  
              context.SaveChanges();
  
              account = account1;
          }
  
          using (var context = new TestContext())
          {
              context.Entry(account).State = EntityState.Deleted;
                      context.SaveChanges();
          }
      }
      catch (Exception e)
      {
          Console.WriteLine(e.ToString());
      }
  
      Console.WriteLine("\nPress any key to exit...");
      Console.ReadLine();
  }
  </code></pre>
  
  <p>When I try to delete the parent entity, it throws:</p>
  
  <blockquote>
    <p>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>
  </blockquote>
  
  <p>I believe my relationship configuration is ok (<a href="http://msdn.microsoft.com/en-us/data/jj591620#CascadeDelete" rel="nofollow noreferrer">followed the documentation</a>). I also searched for <a href="https://stackoverflow.com/questions/12742473/most-efficiently-handling-create-update-delete-with-entity-framework-code-firs">guidelines on deleting detached entities</a>.</p>
  
  <p>I really cannot understand why that delete won't work. I want to avoid loading all the children, deleting them one by one and them deleting the parent, because there must be a better solution than that.</p>