title: MVC 3 EF 4.1 dbContext - Deleting one-to-many data object with non-nullable foreign-key relation
body: <p>I am using MVC 3, EF 4.1, and dbContext.  I need to know how to delete an entity in one-to-many relation with a non-nullable foreign-key.</p>
  
  <p>When I Remove the child entity and execute SaveChanges I get the error:</p>
  
  <p>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>
  
  <p>From other posts, I understand that using Remove(entity) marks the entity for delete.  During SaveChanges, EF sets the the foreign-key to Null and the above error occurs.  </p>
  
  <p>I have found some posts that use DeleteObject on the child entity rather than Remove; however, the DeleteObject approach seems to have been dropped because of addition to dbContext and DbSet.</p>
  
  <p>I have found posts that suggest modifying the EDMX foreign-key relation to be Nullable.  Modifying the EDMX is fine, but whenever an Update Model for Database is done, these changes get nuked and must be reapplied. Not optimal.  </p>
  
  <p>Another post suggested creating a proxy entity with the foreign-key relations set to Nullable but I do not understand that approach. It seems to suffer from the same issue as modifying the EDMX in that the context gets automatically updated when changes to the EDMX are saved.</p>
  
  <p>My simplified model is:</p>
  
  <pre><code>public partial class User
  {
      public User()
      {
          this.UserContacts = new HashSet&lt;UserContact&gt;();
      }
  
      public long userId { get; set; }
      public string userEmail { get; set; }
      public string userPassword { get; set; }
      public string userFirstName { get; set; }
      public string userLastName { get; set; }
       . . .
      public virtual Country Country { get; set; }
      public virtual State State { get; set; }
      public virtual ICollection&lt;UserContact&gt; UserContacts { get; set; }
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public partial class UserContact
  {
      public long userContactId { get; set; }
      public long userContactUserId { get; set; }
      public long userContactTypeId { get; set; }
      public string userContactData { get; set; }
  
      public virtual ContactType ContactType { get; set; }
      public virtual User User { get; set; }
  }
  </code></pre>
  
  <p>The userContactUserId and userContactTypeId are required foreign-keys.</p>
  
  <p>In the dbContext container both Users and UserContact are DbSet.</p>
  
  <p>I have a ViewModel for the User and a ViewModel for UserContact as follows</p>
  
  <pre><code>public class UserContactViewModel
  {
      [HiddenInput]
      public long UserContactId { get; set; }
  
      [HiddenInput]
      public long UserContactUserId { get; set; }
  
      [Display(Name = "Contact")]
      [Required]
      public string ContactData { get; set; }
  
      [Required]
      public long ContactType { get; set; }
  
      [HiddenInput]
      public bool isDeleted { get; set; }
  
  }
  
      public class MyProfileViewModel
      {
  
          [HiddenInput]
          public long UserId { get; set; }
  
          [Required]
          [Display(Name = "First Name")]
          [StringLength(100)]
          public string FirstName { get; set; }
  
          [Required]
          [StringLength(100)]
          [Display(Name = "Last Name")]
          public string LastName { get; set; }
          ....
          public IEnumerable&lt;UserContactViewModel&gt; Contacts { get; set; }
  
  }
  </code></pre>
  
  <p>When saving changes to the user profile, I loop over the list of UserContactViewModel entities to determine which have been added, modified, or deleted.</p>
  
  <pre><code>                    foreach (var c in model.Contacts)
                      {
                          UserContact uc = usr.UserContacts.Single(con =&gt; con.userContactId == c.UserContactId);
                          if (uc != null)
                          {
                              if (c.isDeleted == true)  // Deleted UserContact
                              {
                                  ctx.UserContacts.Remove(uc);  // Remove doesn't work
                              }
                              else  //  Modified UserContact
                              {
                                  uc.userContactData = c.ContactData;
                                  uc.userContactTypeId = c.ContactType;
                                  ctx.Entry(uc).State = EntityState.Modified;
                              }
                          }
                          else  // New UserContact
                          {
                              usr.UserContacts.Add(new UserContact { userContactUserId = model.UserId, userContactData = c.ContactData, userContactTypeId = c.ContactType });
                          }
                      }
  </code></pre>
  
  <p>I'd appreciate any help.</p>