ef core one to many relationship throw exception Cannot add or update a child row

后端 未结 2 1327
南旧
南旧 2021-01-24 03:49

My Shop and Product entities have a one to many relationship, please see my models

public class Product
{
   public int ID { get; set; }
   public string Name {          


        
2条回答
  •  甜味超标
    2021-01-24 04:55

    Declare ShopID as nullable in the Product table, you are doing product.Shop = null;, so it tries to insert ShopID value null in this column. Since you column ShopID in Product table is not nullable, that is why its giving error. Because there is no Id in Shop table which is nullable. So its giving foreign key constraint fails exception. So all you have to do is to make use the nullable foreign key like public int? ShopID.

提交回复
热议问题