简体   繁体   中英

INSERT statement conflicted with the FOREIGN KEY

I'm making a Ticketing System using MVC Entity Framework 5.

How my classes are mapped:

User to Admin is 1 to 0..1 (I can assign some users with admin status but I don't want every user to have it)

Admin to Ticket is 1 to 1 (You can only assign 1 admin(to fix the issue) to a ticket)

User to Ticket is 1 to many (One user can create multiple tickets)

I'm having this issue where if I create a ticket with a user that doesn't have an adminID (Look in here ) It'll give me the following error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Ticket_dbo.Administrator_UserID". The conflict occurred in database "RecreationalServicesTicketingSystem.DAL.IssueContext", table "dbo.Administrator", column 'UserID'. The statement has been terminated.

I think the issue is because of the mapping between "Admin to Tickets". The 1 to 1 relationship forces the condition where if i create a ticket the user must have "adminID" or there's a conflict in the database.

My question is that what mapping should Admin to Tickets be to fix this issue? Maybe 1 to 0..*(1 to 1 or many) would it fix the issue?

Administrator

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

    [Key]
    *public int AdminID { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }* 
}

Ticket.cs

public enum Priority
{
    Low, Med, High
}

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; }
    [ForeignKey("UserID")]
    public virtual User User { get; set; }
    public int UserID { get; set; }


    *public int AdminID { get; set; }*
}

Applied Andy's solution where I made Admin to Ticket a 1:Many relationship (Added public virtual Administrator Administrator { get; set; } in the Ticket.cs)

Error LOOK HERE

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Ticket_dbo.Administrator_UserID". The conflict occurred in database "RecreationalServicesTicketingSystem.DAL.IssueContext", table "dbo.Administrator", column 'UserID'. The statement has been terminated.

Administrator

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

    [Key]
    *public int AdminID { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }* 
}

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; }
    [ForeignKey("UserID")]
    public virtual User User { get; set; }
    public int UserID { get; set; }


    *public int AdminID { get; set; }
    public virtual Administrator Administrator { get; set; }*
}

Your adminID property of Ticket cannot accept nullable int, therefore it must have an admin. I'm guessing you don't want that required in the beginning, it gets assigned later? Should be int? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM