title: Entity Framework, Code First, Update "one to many" relationship with independent associations
body: <p>It took me way too long to find a solution to the scenario described below. What should seemingly be a simple affair proved to be rather difficult. The question is: </p>
  
  <p>Using Entity Framework 4.1 (Code First approach) and "Independent associations" how do I assign a different end to an existing "many to one" relationship in a "detached" scenario ( Asp.Net in my case).</p>
  
  <p><strong>The model:</strong></p>
  
  <p>I realize that using ForeignKey relationships instead of Independent Associations would have been an option, but it was my preference to not have a ForeignKey implementation in my Pocos.</p>
  
  <p>A Customer has one or more Targets:</p>
  
  <pre><code>    public class Customer:Person
  {
      public string Number { get; set; }
      public string NameContactPerson { get; set; }
      private ICollection&lt;Target&gt; _targets;
  
      // Independent Association
      public virtual ICollection&lt;Target&gt; Targets
      {
          get { return _targets ?? (_targets = new Collection&lt;Target&gt;()); }
          set { _targets = value; }
      }
  }
  </code></pre>
  
  <p>A Target has one Customer:</p>
  
  <pre><code>    public class Target:EntityBase
  {
      public string Name { get; set; }
      public string Description { get; set; }
      public string Note { get; set; }
      public virtual Address Address { get; set; }
      public virtual Customer Customer { get; set; }
  }
  </code></pre>
  
  <p>Customer derives from a Person class:</p>
  
  <pre><code>    public class Person:EntityBase
  {        
      public string Salutation { get; set; }
      public string Title { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set  ; }        
      public string Telephone1 { get; set; }
      public string Telephone2 { get; set; }
      public string Email { get; set; }        
  
      public virtual Address Address { get; set; }
  }
  </code></pre>
  
  <p>EntityBase class provides some common properties:</p>
  
  <pre><code>    public abstract class EntityBase : INotifyPropertyChanged
  {
      public EntityBase()
      {
          CreateDate = DateTime.Now;
          ChangeDate = CreateDate;
          CreateUser = HttpContext.Current.User.Identity.Name;
          ChangeUser = CreateUser;
          PropertyChanged += EntityBase_PropertyChanged;
      }
  
      public void EntityBase_PropertyChanged(object sender, PropertyChangedEventArgs e)
      {
          if (Id != new Guid())
          {
              ChangeDate = DateTime.Now;
              ChangeUser = HttpContext.Current.User.Identity.Name;
          }
      }
  
      protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null) handler(this, e);
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      public Guid Id { get; set; }
      public DateTime CreateDate { get; set; }
      public DateTime? ChangeDate { get; set; }
      public string CreateUser { get; set; }
      public string ChangeUser { get; set; }
  }
  </code></pre>
  
  <p><strong>The Context:</strong></p>
  
  <pre><code>    public class TgrDbContext : DbContext
  {
      public DbSet&lt;Person&gt; Persons { get; set; }
      public DbSet&lt;Address&gt; Addresses { get; set; }
      public DbSet&lt;Customer&gt; Customers { get; set; }
      public DbSet&lt;Target&gt; Targets { get; set; }
      public DbSet&lt;ReportRequest&gt; ReportRequests { get; set; }
  
      // If OnModelCreating becomes to big, use "Model Configuration Classes"
      //(derived from EntityTypeConfiguration) instead
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Person&gt;().HasOptional(e =&gt; e.Address);            
          modelBuilder.Entity&lt;Customer&gt;().HasMany(c =&gt; c.Targets).WithRequired(t =&gt; t.Customer);            
      }
  
      public static ObjectContext TgrObjectContext(TgrDbContext tgrDbContext)
      {           
          return ((IObjectContextAdapter)tgrDbContext).ObjectContext;
      }
  }
  </code></pre>