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.