title: MVC with Entity Framework Model Mapping
body: <p>I'm having trouble mapping my models:</p>
  
  <p><strong>User to Admin 1 to 0..1 relationship:</strong></p>
  
  <p>One user can be one kind of admin status or no admin status</p>
  
  <p><strong>Users to Tickets 1 to Many relationship:</strong></p>
  
  <p>Users can open multiple tickets but a ticket can only be assigned to one User. User to Ticket 1 to Many</p>
  
  <p><strong>Administrator to Ticket 1 to Many relationship:</strong></p>
  
  <p>Admin (A user with admin status) has tickets assigned to him to fix. The ticket should have an adminID to identify which admin is assigned to it .</p>
  
  <p>I followed tutorials which already has models (LOOK BELOW). It's my first time creating a MVC with Entity Framework Web Application so I don't know if Circular Mapping is possible.</p>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>"FK_dbo.User_dbo.Administrator_AdministratorID". The conflict occurred
    in database "RecreationalServicesTicketingSystem.DAL.IssueContext",
    table "dbo.Administrator", column 'AdministratorID'. The statement has
    been terminated.</p>
  </blockquote>
  
  <p>User.cs</p>
  
  <pre><code>public class User
  {
      public int UserID { get; set; }
      [StringLength(50, MinimumLength = 1)]
      public string LastName { get; set; }
      [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]
  
      [Column("FirstName")]
      public string FirstMidName { get; set; }
  
      public string FullName
      {
          get { return LastName + ", " + FirstMidName; }
      }
      [DataType(DataType.Date)]
      [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
      public DateTime EnrollmentDate { get; set; }
      public int? AdministratorID { get; set; }
      public virtual Administrator Administrator { get; set; }
      public int DepartmentID { get; set; }
      [ForeignKey("DepartmentID")]
      public virtual Department Department { get; set; }
      public int DepotID { get; set; }
      [ForeignKey("DepotID")]
      public virtual Depot Depot { get; set; }
      public virtual ICollection&lt;Ticket&gt; Tickets { get; set; }
  }
  </code></pre>
  
  <p>Administrator.cs</p>
  
  <pre><code>public class Administrator
  {
      public int AdministratorID { get; set; }
      public string AdministratorTitle { get; set; }
      public virtual ICollection&lt;Ticket&gt; Tickets { get; set; }
  
  }
  </code></pre>
  
  <p>Ticket.cs</p>
  
  <pre><code>public class Ticket
  {
      public int TicketID { get; set; }
      public string Issue { get; set; }
      [DisplayFormat(NullDisplayText = "No Priority")]
      public Priority? Priority { get; set; }     
      [ForeignKey("CategoryID")]
      public virtual Category Category { get; set; }
      public int CategoryID { get; set; }
      public int AdministratorID { get; set; }
      [ForeignKey("UserID")]
      public virtual User User { get; set; }
      public int UserID { get; set; }
  }
  </code></pre>
  
  <p>I followed this 1 to 0..1 model for my (User/Instructor) 1 to 0...1 (Administrator/OfficeAssignment) </p>
  
  <pre><code>public class Instructor
  {
      public Int InstructorID { get; set; }
      public string LastName { get; set; }
      public string FirstMidName { get; set; }
  
      public int? OfficeAssignmentID { get; set; }
      public virtual OfficeAssignment OfficeAssignment { get; set; }
  
      public int? HomeID { get; set; }
      public virtual Home Home { get; set; }
  
  }
  
  public class OfficeAssignment
  {
      public int OfficeAssignmentID { get; set; }
      public string Location { get; set; }
  
  } 
  </code></pre>
  
  <p>I followed this 1 to many model for my (Administrator/Team) 1 to 0...1 (Ticket/Player) </p>
  
  <p>Player.cs</p>
  
  <pre><code>public class Player
  {
      public int PlayerId { get; set; }
      public string Name { get; set; }
      public int TeamId { get; set; }
  
      public virtual Team Team { get; set; } // This is new
  }
  </code></pre>
  
  <p>Team.cs </p>
  
  <pre><code>public class Team
  {
      public int TeamId { get; set; }
      [Required] public string Name { get; set; }
      public string City { get; set; }
      public DateTime Founded { get; set; }
  
      public virtual ICollection&lt;player&gt; Players { get; set; } // This is new
  }
  </code></pre>