gpt4 book ai didi

asp.net-mvc - 多对多关系上的 Entity Framework 错误

转载 作者:行者123 更新时间:2023-12-03 04:18:43 25 4
gpt4 key购买 nike

我有以下类(class)可供预约:

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.

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

我研究了一下,发现问题出在级联删除上,我应该禁用它。问题是我不知道如何做,而且我发现的选项似乎对我不起作用(它们显然适用于一对多关系)。任何解决此问题的帮助将不胜感激。提前致谢....

最佳答案

这应该可以正常工作(您不需要多对多的映射,按照约定工作)。你有哪个版本的EF/CF?但无论如何,既然它不...

a) 尝试删除约定,正如 @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; }
}

...这肯定可以解决问题。尽管您失去了“服务”“约会”直接导航(全部通过服务约会进行)

关于asp.net-mvc - 多对多关系上的 Entity Framework 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15753998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com