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

次の方法で、これら 2 つの間に多対多の関係を作成しようとしています。

 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.

制約を作成できませんでした。以前のエラーを参照してください。

少し調べてみたところ、問題はカスケード削除にあり、無効にする必要があることがわかりました。問題は、方法がわからず、見つけたオプションが機能しないように見えることです (それらは明らかに一対多の関係用です)。この問題を解決する助けをいただければ幸いです。前もって感謝します....

4

2 に答える 2

0
protected override void OnModelCreating( DbModelBuilder modelBuilder )
      {

         modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

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

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

      }
于 2013-04-02T01:16:44.467 に答える