title: MVC EF Setting up relationships
body: <p>I'm trying to map the following:</p>
  
  <p>User to Admin 1 to 0..1 relationship. One user can be one kind of admin status or no admin status</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>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>Can someone verify that my mapping is correct </p>
  
  <p>What my Ticket table looks like <a href="http://i.stack.imgur.com/S7NTL.png" rel="nofollow">CLICK HERE</a></p>
  
  <pre><code>modelBuilder.Entity&lt;Ticket&gt;()
      .HasMany(c =&gt; c.Administrators).WithMany(i =&gt; i.Tickets)
      .Map(t =&gt; t.MapLeftKey("TicketID")
          .MapRightKey("AdministratorID")
          .ToTable("AdministratorsTickets"));
  
  
  modelBuilder.Entity&lt;Administrator&gt;()
             .HasKey(e =&gt; e.UserID);
  
  modelBuilder.Entity&lt;User&gt;()
      .HasOptional(s =&gt; s.Administrator) // Mark StudentAddress is optional for Student
      .WithRequired(ad =&gt; ad.User); // Create inverse relationship
  </code></pre>
  
  <p>Configuration.cs</p>
  
  <pre><code>public Configuration()
  {
      AutomaticMigrationsEnabled = false;
  }
  
  protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
  {
      var departments = new List&lt;Department&gt;
      {
          new Department { DepartmentID = 1, Name = "IT"},
          new Department { DepartmentID = 2, Name = "Admin" },
          new Department { DepartmentID = 3, Name = "Human Resources"},
          new Department { DepartmentID = 4, Name = "Mechanics" },
          new Department { DepartmentID = 5, Name = "Directors" },
          new Department { DepartmentID = 6, Name = "Operations"}
  
      };
      departments.ForEach(s =&gt; context.Departments.AddOrUpdate(p =&gt; p.Name, s));
      context.SaveChanges();
  
  
      var depots = new List&lt;Depot&gt;
      {
          new Depot { DepotID = 1, Name = "Porana"},
          new Depot { DepotID = 2, Name = "Far North"},
  
  
      };
      depots.ForEach(s =&gt; context.Depots.AddOrUpdate(p =&gt; p.Name, s));
      context.SaveChanges();
  
      var users = new List&lt;User&gt;
  {
      new User { FirstMidName = "Jason",   LastName = "Wan",
          EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1},
      new User { FirstMidName = "Andy", LastName = "Domagas",
          EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1,DepotID = 1},
      new User { FirstMidName = "Denis",   LastName = "Djohar",
          EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1 ,DepotID = 1},
      new User { FirstMidName = "Christine",   LastName = "West",
          EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1},
  
  };
  
  
      users.ForEach(s =&gt; context.Users.AddOrUpdate(p =&gt; p.FirstMidName, s));
      context.SaveChanges();
  
      users.ForEach(s =&gt; context.Users.AddOrUpdate(p =&gt; p.LastName, s));
      context.SaveChanges();
  
  
      var administrator = new List&lt;Administrator&gt;
      {
          new Administrator {AdminID = 1, AdminRole = "Administrator LVL1", User = users.Single ( s =&gt; s.UserID == 1),
          Tickets = new List&lt;Ticket&gt;() },
          new Administrator {AdminID = 2, AdminRole = "Administrator LVL2", User = users.Single ( s =&gt; s.UserID == 2),
          Tickets = new List&lt;Ticket&gt;() },
          new Administrator {AdminID = 3, AdminRole = "Administrator LVL3", User = users.Single ( s =&gt; s.UserID == 3),
          Tickets = new List&lt;Ticket&gt;() }
  
      };
      administrator.ForEach(s =&gt; context.Administrators.AddOrUpdate(p =&gt; p.AdminID, s));
      context.SaveChanges();
  
      var categories = new List&lt;Category&gt;
      {
          new Category {CategoryID = 0001, Title = "Desktop"},
          new Category {CategoryID = 0002, Title = "Mobile"},
          new Category {CategoryID = 0003, Title = "Menzits"},
          new Category {CategoryID = 0004, Title = "XMPRO"},
          new Category {CategoryID = 0005, Title = "Con-X"},
          new Category {CategoryID = 0006, Title = "Promapp"},
          new Category {CategoryID = 0007, Title = "QGIS"},
      };
      categories.ForEach(s =&gt; context.Categories.AddOrUpdate(p =&gt; p.Title, s));
      context.SaveChanges();
  
      var tickets = new List&lt;Ticket&gt;
      {
          new Ticket {
              UserID = users.Single(s =&gt; s.LastName == "Wan").UserID,
              CategoryID = categories.Single(c =&gt; c.Title == "Con-X" ).CategoryID,
              Issue = ("Test Error NEW TEST"),
              AdminID = 1,
              Priority = Priority.High
          },
          new Ticket {
              UserID = users.Single(s =&gt; s.LastName == "Wan").UserID,
              CategoryID = categories.Single(c =&gt; c.Title == "Desktop" ).CategoryID,
              Issue = ("Test Error 2"),
              AdminID = 2,
              Priority = Priority.Med
          },
      };
  
  
      foreach (Ticket e in tickets)
      {
          var ticketInDataBase = context.Tickets.Where(
              s =&gt;
                  s.User.UserID == e.UserID &amp;&amp;
                  s.Category.CategoryID == e.CategoryID).SingleOrDefault();
          if (ticketInDataBase == null)
          {
              context.Tickets.Add(e);
          }
      }
      context.SaveChanges();
  }
  </code></pre>
  
  <p>Ticket.cs</p>
  
  <pre><code>public enum Priority
  {
      Low, Med, High
  }
  
  public class Ticket
  {
      public string Issue { get; set; }
      [DisplayFormat(NullDisplayText = "No Priority")]
      public Priority? Priority { get; set; }
      public int CategoryID { get; set; }
      [ForeignKey("CategoryID")]
      public virtual Category Category { get; set; }
      public int TicketID { get; set; }
      public int AdminID { get; set; }
      public virtual ICollection&lt;Administrator&gt; Administrators { get; set; }
      public int UserID { get; set; }
      //[ForeignKey("TicketID")]
      public virtual User User { get; set; }
  }
  </code></pre>
  
  <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; }
  
      [DataType(DataType.Date)]
      [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
      public DateTime EnrollmentDate { get; set; }
  
      public string FullName
      {
          get { return LastName + ", " + FirstMidName; }
      }
      public int AdministratorID { get; set; }
      [ForeignKey("AdministratorID")]
      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 int TicketID { get; set; }
      public virtual ICollection&lt;Ticket&gt; Tickets { get; set; }
  
  }
  </code></pre>
  
  <p>Administrator.cs</p>
  
  <pre><code>public class Administrator
  {
      [Key, ForeignKey("User")]
      public int UserID { get; set; }
      public int AdminID { get; set; }
      public int TicketID { get; set; }        
      [StringLength(50)]
      public string AdminRole { get; set; }
      public virtual ICollection&lt;Ticket&gt; Tickets { get; set; }
      public virtual User User { get; set; }
  }
  </code></pre>
  
  <p>IssueContext.cs</p>
  
  <pre><code>   public class IssueContext : DbContext
      {
          public DbSet&lt;User&gt; Users { get; set; }
          public DbSet&lt;Ticket&gt; Tickets { get; set; }
          public DbSet&lt;Category&gt; Categories { get; set; }
          public DbSet&lt;Department&gt; Departments { get; set; }
          public DbSet&lt;Administrator&gt; Administrators { get; set; }
          public DbSet&lt;Depot&gt; Depots { get; set; }
  
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
  
              modelBuilder.Entity&lt;Ticket&gt;()
                  .HasMany(c =&gt; c.Administrators).WithMany(i =&gt; i.Tickets)
                  .Map(t =&gt; t.MapLeftKey("TicketID")
                      .MapRightKey("AdministratorID")
                      .ToTable("AdministratorsTickets"));
  
  
              modelBuilder.Entity&lt;Administrator&gt;()
                         .HasKey(e =&gt; e.UserID);
  
              modelBuilder.Entity&lt;User&gt;()
                  .HasOptional(s =&gt; s.Administrator) // Mark StudentAddress is optional for Student
                  .WithRequired(ad =&gt; ad.User); // Create inverse relationship
  
  
          }
      }
  </code></pre>