3

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:

private void AddOrderDetail(int productID)
{
    OrderDetail newOrderDetail = new OrderDetail();
    newOrderDetail.Name = "New OrderDetail";

    // Here I have no clue what would be the correct way...Should I do 
    // Approach A(pick related entity from database then assign as related entity to base entity):
    var ProductFromDB = DbContext.Products.SingleOrDefault(p => p.ProductID == productID);
    newOrderDetail.ProductFK = ProductFromDB;

    // Approach B(directly assign the foreign key value to base entity)
    newOrderDetail.ProductID = productID

    DbContext.SaveChange();
} 

By using approach (A), my newOrderDetail failed to save and I looked into SQL resource, looks like it considered the one that I retrieved from the database (ProductFromDB) as a new object and was trying to insert it again. I feel it's redundant job for picking ProductFromDB first then assign to the inserted entity...

By using approach (B), my newOrderDetail still failed to save, and I am getting an exception like "insert A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.", however, this exception does not happen constantly. I looked into the SQL and found the SQL Script, by running it individually in SQL Server it worked, however when running an application side, it's not working...

So what would be the correct way to deal with above scenario?

1 Answer 1

3

If you don't need access to the complete Product object right away, you could try to set just the foreign key column value of your newOrderDetail. Also, you need to ADD your newly created OrderDetail object to the DbContext before saving - something like this:

private void AddOrderDetail(int productID)
{
    OrderDetail newOrderDetail = new OrderDetail();
    newOrderDetail.Name = "New OrderDetail";
    newOrderDetail.ProductID = productID

    // OF COURSE, you need to ADD the newly created object to the context!
    DbContext.OrderDetails.Add(newOrderDetail);

    DbContext.SaveChange();
} 

Next time around, when you actually fetch the OrderDetail, it will resolve the linked product and you should be fine.:

using (var DbContext = new YourDbContext())
{
    OrderDetail od = DbContext.OrderDetails.FirstOrDefault(x => x.Name = "New OrderDetail");
    ....
}
5
  • Thank you so much!
    – Kev D.
    Jun 30, 2018 at 8:30
  • So what if I want to save a new Product with a collection of existed OrderDetails, from API I can get a list of OrderDetailID in an array, should I need to fetch each OrderDetail from database (by OrderDetailID) then add to the Product's OrderDetails Collection? I am not sure that's the correct way but seems like it drops a lot of performance...
    – Kev D.
    Jun 30, 2018 at 8:36
  • @KevinDing: seems like a highly unlikely business scenario - adding a new product to a list of existing orders...... is this just for the sake of discussion, or do you actually have such a business case?? But yes - if you ever need to grab a whole collection to store - of course that will be slower than storing just a single object - no doubt about it.
    – marc_s
    Jun 30, 2018 at 9:51
  • thank you so much! Sorry If I did not describe correctly, this time I was trying to save a single object but it has a collection of its related entities which are already existed in the database, I have opened a new ticket on that [Entity Framework Core - insert entity with multiple related entities which existed in the database][1] [1]: stackoverflow.com/questions/51120596/…
    – Kev D.
    Jul 1, 2018 at 5:33
  • i tried doing this and found that there in total two db calls one for fetching product and other for inserting the new record. any way to avoid this? Sep 24, 2019 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.