如何使用 ef 为迁移构建强制执行 Restrict DeleteBehavior?

How to enforce Restrict DeleteBehavior for migration build using ef?

我有一个 PatientRegistry table 与三个相关的 table 有 one-to-many 关系 CountryStateCity。在迁移期间,默认 DeleteBehavior 设置为 Cascade,这在 database Update 期间给我一个错误。如果我将其更改为 Restrict,我可以正确地 seed。我试图在构建期间强制执行 Restrict 行为,但我在播种期间不断收到此错误,

Unhandled Exception: System.InvalidOperationException: The association between entity types 'City' and 'PatientRegistry' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes.

如何防止构建期间的级联删除行为?

我贴出相关代码,

[Table("PatientsRegistry")]
    public class PatientRegistry
    {   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Display(Name = "Record Id")]
        public long RecordId { get; set; }
        [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
        [Display(Name = "Patient File Number")]
        public long PatientFileId { get; set; }
        public int CountryId { get; set; }
        public Country Country { get; set; }
        public int StateId { get; set; }
        public State State { get; set; }
        public int CityId { get; set; }
        public City City { get; set; }
        [Timestamp]
        public byte[] RowVersion { get; set; }
        public ICollection<PartnerRegistry> Partners { get; set; }
        public PatientRegistry()
        {
            Partners = new Collection<PartnerRegistry>();
        }

    }

在我的 OnModelCreating

        builder.Entity<ApplicationUser>()
                .HasOne(c => c.Country)
                .WithOne()
                .OnDelete(DeleteBehavior.Restrict);

        builder.Entity<ApplicationUser>()
                .HasOne(c => c.State)
                .WithOne()
                .OnDelete(DeleteBehavior.Restrict);

        builder.Entity<ApplicationUser>()
                .HasOne(c => c.City)
                .WithOne()
                .OnDelete(DeleteBehavior.Restrict);

我播种如下,

  if (!context.PatientsRegistry.Any())
                    {

                        context.PatientsRegistry.AddRange(
                             new PatientRegistry
                             {
                                 PatientFileId = 1111,
                                 CountryId = context.Countries.Where(g => g.Name == "Jordan").SingleOrDefault().Id,
                                 StateId = context.States.Where(g => g.Name == "Amman").SingleOrDefault().Id,
                                 CityId = context.Cities.Where(g => g.Name == "Abu Nusair").SingleOrDefault().Id,
                             }

                        );
                        context.SaveChanges();


                    }

完全错过了,应该是WithMany()

        builder.Entity<ApplicationUser>()
                .HasOne(c => c.Country)
                .WithMany()
                .OnDelete(DeleteBehavior.Restrict);

        builder.Entity<ApplicationUser>()
                .HasOne(c => c.State)
                .WithMany()
                .OnDelete(DeleteBehavior.Restrict);

        builder.Entity<ApplicationUser>()
                .HasOne(c => c.City)
                .WithMany()
                .OnDelete(DeleteBehavior.Restrict);