Seeding data in Entity Framework

Im using Entity framework to manage a postgresql database, which has three tables–Products, ProductCategories and ProductImages, these three tables are related to each other through foreign key,for example the ‘ProductCategoryId” of Products table is a foreign key to the “ProductCategories” table, so do the “ProductImageId” which is a foreigh key to the “ProductImages” table.
Here is the data seeding class

public static class DbInitializer
    {
        public static void Initialize(ProductContext context)
        {

            if (context.Products.Any()
            && context.ProductCategories.Any()
            && context.ProductImages.Any())
            {
                return;   // DB has been seeded
            }


            var productCategories = new ProductCategory[]
              {
                new ProductCategory
                    {
        
                        Name = "Tops",
                    },
                new ProductCategory
                    {
                        
                        Name = "Hats",
                    },
                new ProductCategory
                    {
                        Name = "Shoes",
                    },
               //...
          };
         

            //seeding productImages 
            var productImages = new ProductImage[]
                {
                new ProductImage
                    {
                        // Id = 1,
                        Name = "cat-photo_0000.jpg",
                        Description="Wings harness"
                    },
                new ProductImage
                    {
                        // Id = 2,
                        Name = "cat-photo_0001.jpg",
                        Description="Carry Bag Deluxe"
                    },
                 
            };

            //seeding Products
            var products = new Product[]
            {
                new Product
                    {
                        Name = "Angel Wings Harness",
                        Description = "The purrrfect accessory to take your kitty to the next level.",
                        Price = 10,
                  
                    },
                new Product
                    {
                        Name = "Deluxe Carry Bag Orange",
                        Description = "Backpack-style carry bag with dome.",
                        Price = 20,
                       
                    },
             
            };

         

         context.Products.AddRange(products);
         context.ProductCategories.AddRange(productCategories);
         context.ProductImages.AddRange(productImages);

        context.SaveChanges();
        }


    }

Here are the three entities–Product,ProductCategories and ProductImages

{

    public class Product
    {
        // [Key]
        // public int Id { get; set; }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        public string? Name { get; set; }

        public string? Description { get; set; }

        [Column(TypeName = "money")]
        public decimal Price { get; set; }

        [ForeignKey("ProductCategory")]
        public int? ProductCategoryId { get; set; }
        public ProductCategory? ProductCategory { get; set; }

        [ForeignKey("ProductImage")]
        public int? ProductImageId { get; set; }
        public ProductImage? ProductImage { get; set; }
    }


}



public class ProductCategory
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        [MaxLength(100)] // Adjust the maximum length as needed
        public string Name { get; set; }
    }




public class ProductImage
{
    // [Key]
    // public int Id { get; set; }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    [MaxLength(100)] // Adjust the maximum length as needed
    public string? Name { get; set; }

    [MaxLength(500)] // Adjust the maximum length for description
    public string? Description { get; set; }
}

I have seeded some dummy data in the three tables, as you can see the “ProductCategoryId” and “ProductImageId” columns got no values(null) in the “Products” table

After that, I can post new records from the swagger page for example.

Now my question is, how can I populate those “Id” values from “ProductCategories” and “ProductImages” table and apply them into the “Products” table to replace thos NULL values? Looks like a silly question but just need some helps. Thank you.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật