Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce Restrict DeleteBehavior for migration build using ef?

I have a PatientRegistry table that is related to has three related tables with a one-to-many relationship Country, State and City. During migration, the default DeleteBehavior is set to Cascade which gives me an error during database Update. if I change it to Restrict I can seed properly. I am trying to enforce the Restrictbehavior during build but I keep getting this error during seeding,

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.

How can I prevent the cascade delete behavior during build?

I am posting the relative code,

[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>();
        }

    }

and in my 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);

and I seed as follow,

  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();


                    }
like image 786
JSON Avatar asked Dec 08 '25 21:12

JSON


1 Answers

Totally missed it, should be 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);
like image 192
JSON Avatar answered Dec 11 '25 12:12

JSON



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!