我有以下约会课程:
public class Appointment
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int appointmentId { get; set; }
public int Mins { get; set; }
public DateTime StartDateTime { get; set; }
public string Note { get; set; }
//Navagation Properties
public CompanyInformation Company { get; set; }
public virtual ICollection<Service> Services { get; set; }
public UserProfile Customer { get; set; }
public UserProfile Staff { get; set; }
public int? ParentID { get; set; }
public Appointment ParentAppointment { get; set; }
}
和以下服务等级:
public class Service
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int serviceId { get; set; }
public string name { get; set; }
public string description { get; set; }
public decimal price { get; set; }
public bool isPublished { get; set; }
public bool isActive { get; set; }
public virtual CompanyInformation Company { get; set; }
public UserProfile defaultStaff { get; set; }
public virtual ICollection<Appointment> Appointments { get; set; }
}
我试图通过以下方式在这两者之间建立多对多关系:
modelBuilder.Entity<Service>().HasMany(e => e.Appointments).WithMany(e => e.Services);
在 DbContext 的 OnModelCreating 中。当我尝试更新数据库时,出现以下错误。
Introducing FOREIGN KEY constraint 'FK_dbo.ServiceAppointments_dbo.Appointments_Appointment_appointmentId' on table 'ServiceAppointments' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
无法创建约束。请参阅以前的错误。
我对其进行了一些研究,发现问题出在级联删除上,我应该禁用它。问题是我不知道我发现的方法和选项似乎对我不起作用(它们显然适用于一对多关系)。任何解决此问题的帮助将不胜感激。提前致谢....