3

我有约会下面的类:上的许多实体框架错误一对多的关系

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; } 
} 

我试图创建一个多TO-这两者之间有很多关系:

modelBuilder.Entity<Service>().HasMany(e => e.Appointments).WithMany(e => e.Services); 

在OnModelCreating的DbContext中。当我尝试更新数据库时,出现以下错误。

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. 

无法创建约束。查看以前的错误。

我已经研究了一下,发现问题是与级联删除,我应该禁用它。问题是我不知道如何和我发现的选项似乎不适合我(他们显然是一对多的关系)。任何帮助解决这个问题将不胜感激。在此先感谢....

回答

1

这应该很好地工作,因为它是你不需要映射了许多一对多,按照惯例作品)。 你有哪个版本的EF/CF?但无论如何,因为它不...

一)尝试删除该公约作为@Lajos提到的 - 这应该做的伎俩,

B)如果不工作,你可以“改写”手动的关系 - 和关闭级联,这样的事情...

modelBuilder.Entity<Appointment>() 
    .HasOptional(x => x.ParentAppointment) 
    .WithOptionalDependent() 
    .WillCascadeOnDelete(false); 

modelBuilder.Entity<ServiceAppointment>() 
    .HasKey(x => new { x.ServiceId, x.AppointmentId }); 

modelBuilder.Entity<ServiceAppointment>() 
    .HasRequired(x => x.Service) 
    .WithMany(x => x.Appointments) 
    .HasForeignKey(at => at.ServiceId) 
    .WillCascadeOnDelete(false); 

modelBuilder.Entity<ServiceAppointment>() 
    .HasRequired(x => x.Appointment) 
    .WithMany(x => x.Services) 
    .HasForeignKey(at => at.AppointmentId) 
    .WillCascadeOnDelete(false); 

只要改变这两类藏品......

// public virtual ICollection<Appointment> Appointments { get; set; } 
public virtual ICollection<ServiceAppointment> Appointments { get; set; } 

// public virtual ICollection<Service> Services { get; set; } 
public virtual ICollection<ServiceAppointment> Services { get; set; } 

public class ServiceAppointment 
{ 
    public int ServiceId { get; set; } 
    public int AppointmentId { get; set; } 
    public Service Service { get; set; } 
    public Appointment Appointment { get; set; } 
} 

......应该明确地解决问题。虽然你失去了'服务''约会'直接导航(全部通过服务约会)

0
protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 

     modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 

    modelBuilder.Entity<Appointment>() 
     .WithRequiredDependent() 
     .WillCascadeOnDelete(false); 

    modelBuilder.Entity<Service>() 
     .WithRequiredDependent() 
     .WillCascadeOnDelete(false); 

     } 
+0

拉霍斯,另一部分没有工作的方式 - 关于公约的好处。 – NSGaga