TykiMikk
TykiMikk

Reputation: 1068

MVC Entity Relationship Mapping: Do I need a separate class(Admin) or not?

Here is my mapping!

User to Admin 1 to 0..1 relationship??

Admin class has 2 properties: AdministratorID(Int) and AdministratorTitle(String)

One user can be one kind of admin status or no admin status

Users to Tickets 1 to Many relationship??

Users can open multiple tickets but a ticket can only be assigned to one User.

Administrator to Ticket 1 to Many relationship??

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 .

The Ticket table should have: TicketID,AdminID(Admin assigned to fix it) and UserID(Person who created the ticket)

The User table should have : UserID, AdminID(0: Not an admin,1: Lvl 1 Admin, 2: Lvl 2 Admin) and TicketID(All the tickets that the User created)

The Administrator table should have: AdminID, UserID

My Question is do I need an Admin Class or not? Will I be able to show a table which has TicketID,UserID and AdminID(Shows the name of the User who is also an admin)

This is what I've done so far:

User.cs

   public class User
    {
        public int UserID { get; set; }

        public int? AdministratorID { get; set; }
        [ForeignKey("AdministratorID")]
        public virtual Administrator Administrator { get; set; }

        public virtual ICollection<Ticket> Tickets { get; set; }
    }

Administrator.cs

public class Administrator
{
    public int AdministratorID { get; set; }
    public string AdministratorTitle { get; set; }


    //  public virtual ICollection<Ticket> Tickets { get; set; }

    public virtual ICollection<User> Users { get; set; }


}

}

Ticket.cs

public class Ticket
{
    public int TicketID { get; set; }

    public int UserID { get; set; }
    [ForeignKey("UserID")]
    public virtual User User { get; set; }


    public int AdministratorID { get; set; }
    [ForeignKey("AdministratorID")]
    public virtual Administrator Administrator { get; set; }
}

Upvotes: 1

Views: 133

Answers (1)

simon at rcl
simon at rcl

Reputation: 7344

Given that one admin has exactly one user, there is no need for a separate Admin class or table. The User already has an Admin flag, so you know which Users can and can't be assigned to a Ticket. The Ticket class becomes:

public class Ticket
{
    public int TicketID { get; set; }

    //For Ticket to User relationship many to 1
    public int UserID { get; set; }
    public virtual User User { get; set; }

    //For Administrator to Ticket 1 to many relationship
    public int? Administrator{ get; set; }
    public virtual User Administrator { get; set; }

There is an FK relationship between Ticket.AdministratorId and User.Id and Ticket.AdministratorId will, I expect, be nullable to take care of the case when no-one is assigned as yet to the Ticket.

EDIT: As requested by OP:

public class User
    {
        public int UserID { get; set; }

        // No separate Administrator class required
        public bool IsAdministrator { get; set; }

        public virtual ICollection<Ticket> Tickets { get; set; }
    }
public class Ticket
{
    public int TicketID { get; set; }

    public int UserID { get; set; }
    [ForeignKey("UserID")]
    public virtual User User { get; set; }

    // This "Administrator" is a User with IsAdministrator = true
    // A Ticket may or may not have an Administrator hence it is nullable.
    public int? AdministratorID { get; set; }
    [ForeignKey("UserID")]
    public virtual User Administrator { get; set; }
}

Upvotes: 2

Related Questions