title: Setting a foreign key to null when using entity framework code first
body: <p>I'm using the database first implementation of Entity Framework Code First as the data layer for a project, but I've run into a problem.</p>
  
  <p>I need to be able to set a foreign key to null in order to remove an association in the database.</p>
  
  <p>I have 2 objects. One is called Project.</p>
  
  <pre><code>public class Project
  {
      public int ProjectId {get; set;}
      public Employee Employee {get;set;}
  }
  
  public class Employee
  {
      public int EmployeeId {get; set;}
      public string EmployeeName {get;set;}
  }
  </code></pre>
  
  <p>This matches what I have in the Database:</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE Project(
      ProjectId int IDENTITY(1,1) NOT NULL,
      EmployeeId int NULL
  )
  
  CREATE TABLE Project(
      EmployeeId int IDENTITY(1,1) NOT NULL,
      EmployeeName varchar(100) NULL
  )
  </code></pre>
  
  <p>I can assign an Employee to a project. However, I want to be able to remove an employee from a project and have the Employee field be null. In my UI this will show as 'No EMployee Assigned'.</p>
  
  <p>However, short of a direct sql query, I cannot seem to find a way to do this in the entity framework 4.1. </p>
  
  <p>I've tried:</p>
  
  <pre><code>public void RemoveEmployeeFromProject(int projectId)
  {
      var project = Context.Projects.FirstOrDefault(x =&gt; x.ProjectId == projectId);
      project.Employee = null;
      Context.SaveChanges();
  }
  </code></pre>
  
  <p>But this doesn't do anything.</p>
  
  <p>Does anyone have any ideas?</p>