title: ASP.NET MVC5 EF6: Given multiplicity constraints, a corresponding 'User_AppUser_Target' must also in the 'Deleted' state. Repository Pattern
body: <p>I have an MVC5 project using Identity 2 and Entity Framework 6 and the repository pattern, that is redeveloping an old classic asp system.
  I started the project with Identity 1, after which in v2 they introduced that the ApplicationUser primary key can be anything, not just a GUID.</p>
  
  <p>However, I have already mapped the ApplicationUser (AspNetUsers table) to my existing User table from the old project as a 1:1 relationship (as much as EF can). It is really a 1:1 which was necessary because my old table has many many fields.</p>
  
  <p><strong>When I try to delete a User, and it's associated ApplicationUser, I get the following error:</strong></p>
  
  <blockquote>
    <p>A relationship from the 'User_AppUser' AssociationSet is in the
    'Deleted' state. Given multiplicity constraints, a corresponding
    'User_AppUser_Target' must also in the 'Deleted' state.</p>
  </blockquote>
  
  <p><strong>Here is the Application User:</strong></p>
  
  <pre><code>    public class ApplicationUser : IdentityUser
      {
          public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager)
          {                
              var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);             
              return userIdentity;
          }
  
          public virtual User User { get; set; }
      }
  </code></pre>
  
  <p><strong>Here is the User</strong> </p>
  
  <pre><code>public class User : BaseModel
  {        
      // The new identity system has a Guid as Id that is different to the UserId, So on the AspNetUser table there is new UserId field mapped.
      [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int UserId { get; set; }    
      public virtual ApplicationUser AppUser { get; set; }      
      public string UserLogin { get { return AppUser.UserName; } }
      public string UserPass { get { return AppUser.PasswordHash; } }
      public string EmailAddress { get { return AppUser.Email; } }
  
      //.....ETC ....//
  }
  </code></pre>
  
  <p><strong>Here is the mapping:</strong>        </p>
  
  <pre><code>modelBuilder.Entity&lt;User&gt;()                
              .HasRequired(e =&gt; e.AppUser)                
              .WithRequiredPrincipal(e =&gt; e.User)
              .Map(c=&gt;c.MapKey("UserId"))
              .WillCascadeOnDelete(true);
  </code></pre>
  
  <p><strong>Here is the applicationUser table (AspNetUser)</strong>
  I have a foreign key mapping to the User table:</p>
  
  <ol>
  <li>Id nvarchar(128) </li>
  <li>UserName varchart(256) etc..</li>
  <li>UserId int - <em>maps to User Table: UserId int</em></li>
  </ol>
  
  <p><strong>And here is the deleting code:</strong></p>
  
  <pre><code>[HttpPost, ValidateAntiForgeryToken]
  public async Task&lt;ActionResult&gt; Delete([Bind(Include="Id")] UserSaveViewModel viewModel)
  {            
      ApplicationUser appUser = UserManager.FindById(viewModel.Id);                        
      await UserManager.DeleteAsync(appUser);                        
      return RedirectToAction("Index", new { isUserDeleted = true });
  }
  </code></pre>
  
  <p>I have also tried this instead of UserManager.DeleteAsync()</p>
  
  <pre><code>var user = appUser.User;
  await _repository.DeleteAsync&lt;User&gt;(user); //using the repository pattern
  </code></pre>
  
  <p>But that gives this error:</p>
  
  <blockquote>
    <p>The object cannot be deleted because it was not found in the
    ObjectStateManager.</p>
  </blockquote>
  
  <p>How can I get this to cascade delete correctly?</p>