title: EF Code First 4.1 - How to configure one to many relationship with default
body: <p>I have a Customer entity which references a collection of Addresses.  The complication here is that I want to be able to identify a particular address as the default address.</p>
  
  <p>If possible I would like to hold the FK of the default address in the Customer table.  This seems more elegant than having a column in the addresses table to identify the default.</p>
  
  <p>I am having difficulty with the fluent API in terms of defining this relationship.  When I run the following code I get an exception which says:
  <em>"An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details."
  "Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values."</em></p>
  
  <p>I created a console app to show the precise problem.  In this test app I have a Customer entity, an Address and the flient api configuration.</p>
  
  <p>Any help would be much appreciated:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data.Entity.ModelConfiguration;
  using System.ComponentModel.DataAnnotations;
  using System.Data.Entity;
  
  namespace OneToManyWithDefault
  {
  
      public class Customer
      {
          private ICollection&lt;Address&gt; m_Addresses;
  
          public Customer()
          {
              Addresses = new List&lt;Address&gt;();
          }
  
          public int Id { get; set; }
          public string CompanyName { get; set; }
          public virtual ICollection&lt;Address&gt; Addresses
          {
              get
              {
                  if (m_Addresses == null)
                  {
                      m_Addresses = new List&lt;Address&gt;();
                  }
                  return m_Addresses;
              }
              set
              {
                  m_Addresses = value;
              }
          }
          public Address DefaultAddress { get; set; }
          public int DefaultAddressId { get; set; }
  
      }
  
      public class Address
      {
          public int Id { get; set; }
          public string Town { get; set; }
          public Customer Customer { get; set; }
      }
  
      public class MyContext
          : DbContext
      {
          public DbSet&lt;Customer&gt; Customers { get; set; }
  
          public MyContext(string connectionString)
              : base(connectionString)
          {
  
          }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Configurations.Add(new CustomerConfiguration());
              modelBuilder.Configurations.Add(new AddressConfiguration());
              base.OnModelCreating(modelBuilder);
          }
      }
  
      public class CustomerConfiguration
          : EntityTypeConfiguration&lt;Customer&gt;
      {
          public CustomerConfiguration()
              : base()
          {
              HasKey(p =&gt; p.Id);
              Property(p =&gt; p.Id)
                  .HasColumnName("Id")
                  .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                  .IsRequired();
              Property(p =&gt; p.CompanyName)
                  .HasColumnName("Name")
                  .IsRequired();
  
              // Configure the mapping for the Default Address (this is likely to be wrong!):
              HasRequired(p =&gt; p.DefaultAddress).WithMany()
                  .Map(x =&gt; x.MapKey("DefaultAddressId"))
                  .WillCascadeOnDelete(false);
              HasRequired(p =&gt; p.DefaultAddress)
                  .WithMany()
                  .HasForeignKey(x =&gt; x.DefaultAddressId);
  
              ToTable("Customers");
          }
      }
  
      public class AddressConfiguration
          : EntityTypeConfiguration&lt;Address&gt;
      {
          public AddressConfiguration()
              : base()
          {
              HasKey(p =&gt; p.Id);
              Property(p =&gt; p.Id)
                  .HasColumnName("Id")
                  .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                  .IsRequired();
              Property(p =&gt; p.Town)
                  .HasColumnName("Town")
                  .IsRequired();
  
              HasRequired(p =&gt; p.Customer)
                  .WithMany(c =&gt; c.Addresses)
                  .Map(x =&gt; x.MapKey("CustomerId"));
  
              ToTable("Addresses");
          }
      }
  
      class Program
      {
          private const string ConnectionString =
              @"Server=.\sql2005;Database=OneToManyWithDefault;integrated security=SSPI;";
  
          static void Main(string[] args)
          {
              Customer headOffice = new Customer();
              headOffice.CompanyName = "C1";
  
              Address address = new Address();
              address.Town = "Colchester";
              headOffice.Addresses.Add(address);
  
              address = new Address();
              address.Town = "Norwich";
              headOffice.Addresses.Add(address);
              headOffice.DefaultAddress = address;
  
              MyContext context = new MyContext(ConnectionString);
              context.Customers.Add(headOffice);
              context.SaveChanges();
  
              Console.WriteLine("Done.");
              Console.ReadLine();
          }
      }
  }
  </code></pre>
  
  <p>Many thanks,</p>
  
  <p>Paul.</p>