如何使用ef强制限制DeleteBehavior进行迁移构建?

时间:2018-02-16 02:53:51

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

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

  

未处理的异常:System.InvalidOperationException:关联   实体类型之间的城市'和' PatientRegistry'已被切断但是   此关系的外键不能设置为null。如果   应删除依赖实体,然后设置要使用的关系   级联删除。

如何在构建期间阻止级联删除行为?

我发布了相关代码,

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


                    }

1 个答案:

答案 0 :(得分:2)

完全错过了,应该是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);