I get the following error when i try to seed my database.
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Products_Suppliers".The conflict occured in database "Kungu", table "dbo.Suppliers", column 'SupplierId'. The statement has been terminated
the entities classes are Supplier and Product.
Supplier Entity
namespace Kungu.Domain.Entities.Shop
{
public partial class Supplier
{
public Supplier()
{
Products = new HashSet<Product>();
}
[Key]
public int SupplierId { get; set; }
[StringLength(40)]
public string CompanyName { get; set; } = null!;
[StringLength(30)]
public string? ContactName { get; set; }
[StringLength(30)]
public string? ContactTitle { get; set; }
[StringLength(60)]
public string? Address { get; set; }
[StringLength(15)]
public string? City { get; set; }
[StringLength(15)]
public string? Region { get; set; }
[StringLength(10)]
public string? PostalCode { get; set; }
[StringLength(15)]
public string? Country { get; set; }
[StringLength(24)]
public string? Phone { get; set; }
[Column(TypeName = "ntext")]
public string? HomePage { get; set; }
public int AffiliationId { get; set; }
[InverseProperty("Supplier")]
public virtual ICollection<Product> Products { get; set; }
public virtual Affiliations? Affiliation { get; set; }
}
}
Product Entity
namespace Kungu.Domain.Entities.Shop
{
public partial class Product
{
public Product()
{
//Suppliers = new HashSet<Supplier>();
OrderDetails = new HashSet<OrderDetail>();
BookingDetails = new HashSet<BookingDetails>();
PackagesProductsSuppliers = new HashSet<PackagesProductsSuppliers>();
}
[Key]
public int ProductId { get; set; }
[StringLength(40)]
public string ProductName { get; set; } = null!;
public string? Description { get; set; }
public string? ImageURL { get; set; }
public int SupplierId { get; set; }
public int CategoryId { get; set; }
[StringLength(20)]
public string? QuantityPerUnit { get; set; }
[Column(TypeName = "money")]
public decimal Price { get; set; }
public int Quantity { get; set; }
public short? UnitsInStock { get; set; }
public short? UnitsOnOrder { get; set; }
public short? ReorderLevel { get; set; }
public bool Discontinued { get; set; }
[ForeignKey("CategoryId")]
[InverseProperty("Products")]
public virtual Category? Category { get; set; }
[ForeignKey("SupplierId")]
[InverseProperty("Products")]
public virtual Supplier? Supplier { get; set; }
[InverseProperty("Product")]
public virtual ICollection<OrderDetail> OrderDetails {get;set;}
//public virtual ICollection<Supplier> Suppliers { get; set; }
public virtual ICollection<BookingDetails> BookingDetails { get; set; }
public virtual ICollection<PackagesProductsSuppliers> PackagesProductsSuppliers { get; set; }
}
}
What am i doing wrong?
this is how i am seeding the database at first i forgot to add the FK SupplierId but after adding it their is stiil no change.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//Products
//Beauty Category
modelBuilder.Entity<Product>().HasData(new Product
{
ProductId = 1,
ProductName = "Glossier - Beauty Kit",
Description = "A kit provided by Glossier, containing skin care, hair care and makeup products",
ImageURL = "/Images/Beauty/Beauty1.png",
Quantity = 100,
SupplierId = 1,
CategoryId = 1,
QuantityPerUnit = "1 box",
Price = 100
});
modelBuilder.Entity<Product>().HasData(new Product
{
ProductId = 2,
ProductName = "Curology - Skin Care Kit",
Description = "A kit provided by Curology, containing skin care products",
ImageURL = "/Images/Beauty/Beauty2.png",
Quantity = 45,
SupplierId = 1,
CategoryId = 1,
QuantityPerUnit = "1 box",
Price = 50