title: C# MVC EF - Set a foreign key to NULL?
body: <p>I am trying to set a foreign key to "Null".. Using the SQL Management Studio it works fine, but trying it using C#, I am getting a exception:</p>
  
  <p>Exception:</p>
  
  <blockquote>
    <p>Value cannot be null. Parameter name: entity.</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>    var entity = _db.AlleBenutzer.FirstOrDefault(p =&gt; p.Id == id);
      if (entity != null)
      {
          var abteilungObjekt = _db.AlleAbteilungen.FirstOrDefault(p =&gt; p.Abteilungsname == abteilung);
          var etageObjekt = _db.AlleEtagen.Include(p =&gt; p.Abteilung).FirstOrDefault(p =&gt; p.Etagenname == etage);
          entity.Abteilung = abteilungObjekt;
          entity.Etage = etageObjekt;
          _db.Entry(entity.Abteilung).State = EntityState.Modified;
          _db.Entry(entity.Etage).State = EntityState.Modified;
          _db.Entry(entity).State = EntityState.Modified;
          _db.SaveChanges();
      }
  </code></pre>
  
  <p>My both models look like that: (cut some pieces)</p>
  
  <pre><code>public class Benutzer
  {
      public int Id { get; set; }
      public Abteilung Abteilung { get; set; }
      public Etage Etage { get; set; }
  }
  
  public class Abteilung
  {
      public int Id { get; set; }
      public string Abteilungsname { get; set; }
  }
  </code></pre>
  
  <p>Can anyone help me out? How do I modify my model to be able to make my Foreign Keys nullable? Thanks in advance :)</p>