title: Entity Framework Core update column with foreign key reference
body: <p>This is the simplified version of the table structure I have:</p>
  
  <pre><code>[Table("PolicyMapping")]
  public class PolicyMapping
  {
      [Key]
      public Guid Id { get; set; }
      public Policy PolicyA { get; set; }
      public Policy PolicyB { get; set; } 
      public Lookup_Bank Bank { get; set; }       
  }
  
  [Table("Policy")]
  public class Policy
  {
      [Key]
      public Guid Id { get; set; }        
      public string PolicyNm { get; set; }    
      public string Description { get; set; } 
  }
  
  [Table("Lookup_Bank")]
  public class Lookup_Bank
  {
      [Key]
      public Guid Id { get; set; }        
      public string Name { get; set; }            
      public string Code { get; set; }            
  }
  </code></pre>
  
  <p>I am working on the edit screen for policy mapping where you can have the same values for PolicyA and PolicyB attributes.</p>
  
  <p>After using automapper for DTO to the entity, here is my entity object looks like:</p>
  
  <pre><code>var policyMapping = new PolicyMapping
  {
      Id = "b27fb632-330b-46be-a649-2e2463d58626",
      PolicyA = new Policy
      {
          Id = "a4f1cf6f-034d-4727-ab8f-49e95b2c9d23",
          PolicyNm = null,
          Description = null
      },
      PolicyB = new Policy
      {
          Id = "a4f1cf6f-034d-4727-ab8f-49e95b2c9d23",
          PolicyNm = null,
          Description = null
      },
      Bank = new Lookup_Bank()
      {
          Id = "98ed2bae-631b-490c-8ddf-3e02232d4231",
          Name = null,
          Code = null
      }
  }
  </code></pre>
  
  <p>I am mapping only selected id value of dropdown to entity id using automapper. Values are present for <code>Code</code>, <code>Description</code> and other attributes in the database. It's just not getting populated after automapper.</p>
  
  <pre><code>dbContext.PolicyMapping.Attach(policyMapping);
  dbContext.SaveChanges();
  </code></pre>
  
  <p>This is the error, I am getting</p>
  
  <blockquote>
    <p>The instance of entity type <code>Policy</code> cannot be tracked because another instance with the same key value for {'a4f1cf6f-034d-4727-ab8f-49e95b2c9d23'} is already being tracked.<br>
    When attaching existing entities, ensure that only one entity instance with a given key value is attached. </p>
  </blockquote>
  
  <p>The reason for the error maybe because I am attaching two different entities with the same Id. I am still not sure how can I make it work in the most efficient way?</p>
  
  <p><strong>Solution 1</strong>: (not efficient)</p>
  
  <pre><code>var fromdatabase = dbContext.PolicyMapping.Include(x =&gt; x.PolicyA)
          .Include(x =&gt; x.Bank)
          .Include(x =&gt; x.PolicyB)        
          .FirstOrDefault(x =&gt; x.Id == policyMapping.Id);
  
  fromdatabase.PolicyA = dbContext.Policy.FirstOrDefault(x =&gt; x.Id == policyMapping.PolicyA.Id);
  fromdatabase.PolicyB = dbContext.Policy.FirstOrDefault(x =&gt; x.Id == policyMapping.PolicyB.Id);
  
  dbContext.PolicyMapping.Attach(fromdatabase);
  dbContext.SaveChanges();
  </code></pre>
  
  <p>This is working. But I would like to avoid a trip to the database just to fetch the entire entity.</p>
  
  <p><strong>Edit: Based on Xing's answer</strong></p>
  
  <p>@Xing, pointed out to change the Model structure by adding navigational properties and some changes in <code>OnModelCreating</code> method. (This method is currently blank in my code)</p>
  
  <p>However, I went through a couple of articles (<a href="https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db?tabs=visual-studio" rel="nofollow noreferrer">This</a> &amp; <a href="https://www.learnentityframeworkcore.com/walkthroughs/aspnetcore-application" rel="nofollow noreferrer">This</a>) related to EF Core Code First approach, none of them are saying about navigational properties and all.</p>
  
  <p>I am wondering how they are updating the column in this scenario?</p>