Ozdemir Selcuk

Ozdemir Selcuk

  • NA
  • 36
  • 579

I'm getting this error while updating the db

Aug 24 2020 1:41 PM
  1. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)    
  2. {    
  3.     optionsBuilder.UseSqlServer(    
  4.         @"Server=AHMET;Database=test;Trusted_Connection=True");    
  5. }    
  6. public DbSet<Product> Products { getset; }    
  7. public DbSet<ProductFavorite> ProductFavorites{ getset; }    
  8. public DbSet<ProductImage> ProductImages { getset; }    
  9. public DbSet<Category> Categories { getset; }    
  10. public DbSet<SubCategory> SubCategories { getset; }    
  11. public DbSet<Member> Members { getset; }    
  12. public DbSet<User> Users { getset; }    
  13. public DbSet<Blog> Blogs { getset; }    
  14. protected override void OnModelCreating(ModelBuilder modelBuilder)    
  15. {    
  16.     base.OnModelCreating(modelBuilder);    
  17.     
  18. }   
  19.    
  20. public class Category    
  21. {    
  22.     [Key]    
  23.     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]    
  24.     public Guid CategoryId { getset; }    
  25.     [Required]    
  26.     [DisplayName("Category")]    
  27.     public string Name { getset; }    
  28.     public virtual IList<SubCategory> SubCategory { getset; }    
  29. }   
  30. public class SubCategory    
  31. {    
  32.     [Key]    
  33.     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]    
  34.     public Guid SubCategoryId { getset; }    
  35.     [Required]    
  36.     [DisplayName("Subcategory")]    
  37.     public string Name { getset; }    
  38.     [DisplayName("Category")]    
  39.     public Guid CategoryId { getset; }    
  40.     [ForeignKey("CategoryId")]    
  41.     public virtual Category Category { getset; }    
  42.     public virtual IList<Product> Products { getset; }    
  43. }  
I give migrations the update-database command, I get the following error:
 
Error Number:1767,State:0,Class:16 "Foreign key 'FK_Products_Categories_CategoryId' references invalid table 'Categories'. Could not create constraint. See previous errors."
 
Thanks in advance for your interest.

Answers (1)