如何处理“ FOREIGN KEY”迁移错误?

时间:2019-06-17 14:59:39

标签: c# entity-framework .net-core foreign-keys ef-migrations

我正在使用EF Core Code First和通用存储库。 到现在为止,我所做的每个迁移都按预期工作。我无法再进行迁移,因为收到此错误

Failed executing DbCommand (47ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [Appointments] ADD CONSTRAINT [FK_Appointments_AppointmentIntervals_AppointmentIntervalId] FOREIGN KEY ([AppointmentIntervalId]) REFERENCES [AppointmentIntervals] ([AppointmentIntervalId]) ON DELETE CASCADE;
Introducing FOREIGN KEY constraint 'FK_Appointments_AppointmentIntervals_AppointmentIntervalId' on table 'Appointments' 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.

模型任命

        [Key]
        public Guid AppointmentId { get; private set; }

        public Guid PatientId { get; private set; }
        [ForeignKey("PatientId")]
        public Patient Patient { get; private set; }

        public Guid DoctorId { get; private set; }
        [ForeignKey("DoctorId")]
        public Doctor Doctor { get; private set; }

        public DateTime AppointmentDate { get; private set; }

        public Guid? AppointmentIntervalId { get; private set; }
        [ForeignKey("AppointmentIntervalId")]
        public virtual AppointmentInterval AppointmentInterval { get; private set; }

        public bool HaveFeedback { get; private set; }

模型任命间隔

        [Key]
        public Guid AppointmentIntervalId { get; private set; }
        public int Day { get; private set; }
        public TimeSpan StartHour { get; private set; }
        public TimeSpan EndHour { get; private set; }

        public Guid DoctorId { get; private set; }
        [ForeignKey("DoctorId")]
        public Doctor Doctor => null;

        public virtual List<Appointment> Appointments { get; private set; }

我知道它没有用,但是我尝试了这些解决方案,并指定了DeleteBehavior

 public DatabaseService(DbContextOptions<DatabaseService> options) : base(options)
        {
            //context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
            Database.EnsureCreated();
        }

        public DbSet<Patient> Patients { get; set; }
        public DbSet<Doctor> Doctors { get; set; }
        public DbSet<PatientHistory> PatientHistories { get; set; }
        public DbSet<Appointment> Appointments { get; set; }
        public DbSet<Feedback> Feedbacks { get; set; }
        public DbSet<BloodDonor> BloodDonors { get; set; }
        public DbSet<AppointmentInterval> AppointmentIntervals { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {

            foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
            {
                relationship.DeleteBehavior = DeleteBehavior.SetNull;
            }


            modelBuilder.Entity<Appointment>().HasOne(t => t.AppointmentInterval).WithMany(s => s.Appointments)
                .HasForeignKey(bc => bc.AppointmentIntervalId).OnDelete(DeleteBehavior.Restrict);

        }

编辑:

REPOSITORY GetAllAsync

public async Task<List<TEntity>> GetAllAsync(string[] includes)
        {  
            var query = DatabaseService.Set<TEntity>().AsQueryable();
            if (includes != null)
            {
                foreach (string include in includes)
                {

                    query = query.Include(include).AsNoTracking();

                }
            }

            return await query.ToListAsync();

        }

我刚刚为模型添加了新属性,并且无法迁移数据库。

0 个答案:

没有答案