title: Foreign key not populating in Entity Framework
body: <p>I cannot get a table to update correctly that should be linking two of my entities. To explain in more detail...I have two entities, <strong>Class</strong> and <strong>Teacher</strong>, with a relationship in the form of:</p>
  
  <ol>
  <li><em>Teacher can be assigned to many classes</em></li>
  <li><em>Class can only have one teacher.</em></li>
  </ol>
  
  <p>Below are these two entities.</p>
  
  <pre><code>public class Teacher
  {
      [Required, Key]
      public Guid Id { get; private set; }
      [StringLength(50)]
      public string Name { get; set; }
      public string Email { get; set; }
      public List&lt;Class&gt; Classes = new List&lt;Class&gt;();
  
      public Teacher()
      {
          Id = new Guid();
      }
  
      public Teacher(Guid id)
      {
          Id = id;
      }
  
      public void AssignClass(Class newClass)
      {
          Classes.Add(newClass);
      }
  }
  
  public class Class
  {
      [Required, Key]
      public Guid Id { get; private set; }
      [Required, StringLength(20)]
      public string Name { get; set; }
      [Required, Range(5, 30)]
      public int Capacity { get; set; }
      public Teacher Teacher { get; set; }
      public IEnumerable&lt;StudentClass&gt; StudentClasses { get; set; }
  
      public Class()
      {
          Id = new Guid();
      }
  
      public Class(Guid id)
      {
          Id = id;
      }
  }
  </code></pre>
  
  <p>When I generate my migrations I get a foreign key of <strong>TeacherId</strong> in the Classes table as expected. Here is the SQL:</p>
  
  <pre><code>CREATE TABLE [dbo].[Classes] (
  [Id]        UNIQUEIDENTIFIER NOT NULL,
  [Name]      NVARCHAR (20)    NOT NULL,
  [Capacity]  INT              NOT NULL,
  [TeacherId] UNIQUEIDENTIFIER NULL,
  CONSTRAINT [PK_Classes] PRIMARY KEY CLUSTERED ([Id] ASC),
  CONSTRAINT [FK_Classes_Teachers_TeacherId] FOREIGN KEY ([TeacherId]) REFERENCES [dbo].[Teachers] ([Id])
  );
  GO
  CREATE NONCLUSTERED INDEX [IX_Classes_TeacherId]
  ON [dbo].[Classes]([TeacherId] ASC);
  </code></pre>
  
  <p>My class derived of <strong>DBContext</strong> looks like:</p>
  
  <pre><code>public class SchoolDatabaseContext : DbContext
  {
      public DbSet&lt;Student&gt; Students { get; private set; }
      public DbSet&lt;Class&gt; Classes { get; private set; }
      public DbSet&lt;Teacher&gt; Teachers { get; private set; }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
  
      }
  
      public SchoolDatabaseContext(DbContextOptions&lt;SchoolDatabaseContext&gt; options) : base(options)
      {
      }
  }
  </code></pre>
  
  <p>No configuration for those entities yet. I use DI to serve the <strong>DbContext</strong> to the controller and that all seems fine.</p>
  
  <p>I have aimed for a DDD type structure, but to make this issue easier to debug I have stripped everything all the way back to the controller so it is basically... <strong>controller => DbContext</strong>.</p>
  
  <p>Here is my code in the controller:</p>
  
  <pre><code>[HttpPost]
      [Route("assign-teacher-to-class")]
      public async Task&lt;IActionResult&gt; AssignClass([FromBody] AssignTeacherToClass assignTeacherToClass)
      {
          if (!ModelState.IsValid)
          {
              return BadRequest(ModelState);
          }
  
          var teacher = await schoolDatabaseContext.Teachers.FindAsync(assignTeacherToClass.TeacherId);
  
          var classToAssign = await schoolDatabaseContext.Classes.FindAsync(assignTeacherToClass.ClassId);
  
  
          teacher.AssignClass(classToAssign);
  
          schoolDatabaseContext.Entry(teacher).State = EntityState.Modified;
  
          await schoolDatabaseContext.SaveChangesAsync();
  
          return Ok(teacher);
  }
  </code></pre>
  
  <p>When I debug through the ids are fine from the post body, they are assigned correctly to the DTO <strong>AssignClass</strong> and the calls to the <strong>DbContext</strong> to find the data for each type (teacher and class) are fine. I then call a method in my teacher type to add the class to the <strong>List Classes</strong> property (see teachers entity code at beginning for reference), I then Save the changes with the <strong>DbContext</strong> method and <strong>Problem Defined Here:</strong> at no stage does the <strong>TeacherId</strong> in the database update whilst debugging/completing. I have tried all I can think of like instantiating collections in different ways, changing collection types, looking for config that might help map these entities in this way, stripping out all extra layers, changing accessibility of properties and classes and few more. </p>
  
  <p>Any help would really be appreciated as I am getting a bit defeated on this one and I feel like this relationship should be fairly straight forward. I actually was able to get my many to many working with a bridge class so I was surprised to get stuck on this one :(</p>
  
  <p>Thanks</p>