title: Entity Framework Code First Fluent API configuration for one to one identifying relationship
body: <p>I have the following class structure:</p>
  
  <p><img src="https://i.stack.imgur.com/KyQs8.png" alt="enter image description here"></p>
  
  <p>How to configure Fluent API to put identifying relationship in Cards table? </p>
  
  <p>I mean </p>
  
  <ul>
  <li>Cards Table PK: Id, CustomerId</li>
  <li>Cards Table FK: CustomerId</li>
  </ul>
  
  <p>I would like the previous Card be deleted when I assign a new one to Customer.Card property.</p>
  
  <p>So I defined my classes this way:</p>
  
  <pre><code>public class Customer
  {
      public int Id { get; private set; }
      public virtual Card Card { get; set; }
  }
  
  public abstract class Card
  {
      public int Id { get; private set; }
  }
  
  public class Visa : Card
  {
  }
  
  public class Amex : Card
  {
  }
  </code></pre>
  
  <p>DbContext looks like this:</p>
  
  <pre><code>public class Context : DbContext
  {
      public DbSet&lt;Customer&gt; Customers { get; set; }
      public DbSet&lt;Card&gt; Cards { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Entity&lt;Customer&gt;()
              .HasRequired(c =&gt; c.Card)
              .WithRequiredPrincipal()
              .Map(a =&gt; a.MapKey("CustomerId"))
              .WillCascadeOnDelete();
  
          modelBuilder.Entity&lt;Card&gt;();
      }
  }
  </code></pre>
  
  <p>Here is the test:</p>
  
  <pre><code>[TestClass]
  public class UnitTest1
  {
      [TestMethod]
      public void TestMethod1()
      {
          var context = new Context();
          var customer = new Customer();
          context.Customers.Add(customer);
          customer.Card = new Visa();
          context.SaveChanges();
  
          customer.Card = new Amex();
          context.SaveChanges();
  
          Assert.AreEqual(1, context.Customers.Count());
          Assert.AreEqual(1, context.Cards.Count());
      }
  }
  </code></pre>
  
  <p>It does not work at all. I have this on second save and I do not know how to specify identifying relationship here:</p>
  
  <blockquote>
    <p>Unhandled Exception:
    System.Data.Entity.Infrastructure.DbUpdateException: An err or
    occurred while saving entities that do not expose foreign key
    properties for their relationships. The EntityEntries property will
    return null because a singl e entity cannot be identified as the
    source of the exception. Handling of except ions while saving can be
    made easier by exposing foreign key properties in your entity types.
    See the InnerException for details. ---> System.Data.Entity.Core.U
    pdateException: A relationship from the 'Customer_Card' AssociationSet
    is in the  'Deleted' state. Given multiplicity constraints, a
    corresponding 'Customer_Card
    _Target' must also in the 'Deleted' state.</p>
  </blockquote>
  
  <p><strong>UPDATE</strong> It's easy to make it work for one-to-many relationships. You can find a full example below:</p>
  
  <pre><code>[TestClass]
  public class UnitTest1
  {
      [TestMethod]
      public void TestMethod1()
      {
          var context = new Context();
          var customer = new Customer();
          context.Customers.Add(customer);
          customer.Cards.Add(new Visa());
          context.SaveChanges();
  
          customer.Cards[0] = new Amex();
          context.SaveChanges();
  
          Assert.AreEqual(1, context.Cards.Count());
      }
  }
  
  public class Customer
  {
      public Customer()
      {
          Cards = new List&lt;Card&gt;();
      }
  
      public int Id { get; private set; }
      public virtual List&lt;Card&gt; Cards { get; set; }
  }
  
  public abstract class Card
  {
      public int Id { get; private set; }
      public int CustomerId { get; private set; }
  }
  
  public class Visa : Card
  {
  }
  
  public class Amex : Card
  {
  }
  
  public class Context : DbContext
  {
      static Context()
      {
          Database.SetInitializer(new DropCreateDatabaseAlways&lt;Context&gt;());
      }
  
      public DbSet&lt;Customer&gt; Customers { get; set; }
      public DbSet&lt;Card&gt; Cards { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Entity&lt;Customer&gt;()
              .HasMany(c =&gt; c.Cards)
              .WithRequired()
              .HasForeignKey(c =&gt; c.CustomerId)
              .WillCascadeOnDelete();
  
          modelBuilder.Entity&lt;Card&gt;()
              .HasKey(c =&gt; new { c.Id, c.CustomerId });
      }
  }
  </code></pre>