Insert entity with one related entity which existed in the database
Insert entity with one related entity which existed in the database I am stuck at the operation when using Entity Framework Core 2 to perform an insert of a new entity with one related entity which already existed in the database. For example, I have two objects with one to many relationship: public Class OrderDetail { public int ParentID {get;set;} public string OrderDetailName {get;set;} public int ProductID {get;set;} public virtual Product ProductFK {get;set;} } public Class Product { public int ProductID {get;set;} public string ProductName {get;set;} public virtual Collection<OrderDetail> OrderDetails {get;set;} = new Collection<OrderDetail>(); } I would like to add a new OrderDetail with an existing Product (with productID = 3 ) into the database, so I perform like: OrderDetail Product productID = 3 private void AddOrderDetail(int productID) { OrderDetail newOrderDetail = new OrderDetail(); newOrderDetail.Name = "New OrderDetail...