title: EF - Code First - Having Multiple One to Zero or One Relationships
body: <p>i'm developing some scheduling classes and i hit a brick wall after creating multiple One to Zero or One relationships with code first.</p>
  
  <p>Agenda Class</p>
  
  <pre><code>public class Agenda
  {
      public int AgendaId { get; set; }
  
      //Possible Owners of this Agenda
      public int? RoomId { get; set; }
      public Room Room { get; set; }
  
      public int? TrainerId { get; set; }
      public Trainer Trainer { get; set; }
  }
  </code></pre>
  
  <p>Room Class</p>
  
  <pre><code>public class Room
  {
      public int RoomId { get; set; }
  
      public string description { get; set; }
  
      //Without the [Required] gives me an error
      //Unable to determine the principal end of an association between the types...
      public int AgendaId { get; set; }
      [Required]
      public Agenda Agenda { get; set; }
  }
  </code></pre>
  
  <p>Trainer Class</p>
  
  <pre><code>public class Trainer
  {
      public int TrainerId { get; set; }
  
      public string name { get; set; }
  
      //Without the [Required] gives me an error
      //Unable to determine the principal end of an association between the types...
      public int AgendaId { get; set; }
      [Required]
      public Agenda Agenda { get; set; }
  }
  </code></pre>
  
  <p>I need to always have an Agenda when i create a Room or a Trainer, and an Agenda can only belong to a Room or a Trainer and never to both.</p>
  
  <p>So far i managed to create the relationships but now i faced another problem, when i try to insert a new record to the tables Room or Trainer is says:</p>
  
  <blockquote>
    <p>Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF</p>
  </blockquote>
  
  <p>Code First just created both ends as principals/dependent on each other and for example: i cannot insert a new Room/Trainer without an Agenda, nor an Agenda without a Room/Trainer.</p>
  
  <p>Thank you all for your help.</p>