I have 4 model class in my Asp.Net Mvc project. I'm using Entity Framework Code First (Automatic Migration). I when run command update-database, I get following error;
Introducing FOREIGN KEY constraint 'FK_dbo.Tickets_dbo.Users_CreatedUserId' on table 'Tickets' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.
What is the problem?
//Company Class
public class Company
{
public int Id { get; set; }
[Required, StringLength(100)]
public string Title { get; set; }
public virtual ICollection<User> Users { get; set; } = new HashSet<User>();
}
//User Class
public class User
{
public int Id { get; set; }
[Required, StringLength(50)]
public string UserName { get; set; }
[Required, StringLength(100)]
public string FullName { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
}
//Ticket Class
public class Ticket
{
public int Id { get; set; }
[Required]
public DateTime CreationDate { get; set; }
[Required, StringLength(100)]
public string Title { get; set; }
public bool IsClosed { get; set; }
public int CreatedUserId { get; set; }
public virtual User CreatedUser { get; set; }
public virtual ICollection<TicketMessage> Messages { get; set; } = new HashSet<TicketMessage>();
}
//TicketMessage Class
public class TicketMessage
{
public int Id { get; set; }
public DateTime Date { get; set; }
[Required]
public string Message { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public int TicketId { get; set; }
public virtual Ticket Ticket { get; set; }
}
Users
has manyTickets
and also manyTicketMessages
and simultaneouslyTicket
has manyTicketMessages
. – SzerUser
. You should do as it literally said: specify whetherON DELETE NO ACTION
or make customdelete trigger
– Szer