Error deleting a table row splitted on multiple entities

前端 未结 6 997
耶瑟儿~
耶瑟儿~ 2021-01-06 05:18

I want to delete a table row that is split on two entities.

If I try to delete the main entity, I get an error if before I don\'t load the related other entity using

6条回答
  •  梦毁少年i
    2021-01-06 05:30

    Replace your override method with this code and check

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
           modelBuilder.Entity() // one-to-one
               .ToTable("Products")
               .HasKey(e => e.Id)
               .HasRequired(e => e.Info)
               .WithRequiredDependent(e => e.Product);
    
           modelBuilder.Entity() // map to the same table Products
               .ToTable("Products")
               .HasKey(e => e.Id);
    
           //add this code
           modelBuilder.Entity()
                .HasRequired(p => p.Photo) // "Photo"  defined in Product class for ProductPhoto class's object name
                .WithRequiredPrincipal(c => c.Product);// "Product"  defined in ProductPhoto class for Product's class object name
    
    
                base.OnModelCreating(modelBuilder);
    }
    

提交回复
热议问题