How to enforce Restrict DeleteBehavior for migration build using ef?

asp.net-core-2.0 c# ef-migrations entity-framework entity-framework-core

Question

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


                    }
1
1
2/16/2018 3:02:09 AM

Accepted Answer

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);
2
2/16/2018 3:15:13 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow