Does EF upsert have to be done manually?

前端 未结 4 662
忘了有多久
忘了有多久 2021-02-01 04:39

I want to upsert reference members of an existing entity.

Do I have to write specific code for the upsert?

meaning: I have to check if I\'m handling an existing

4条回答
  •  囚心锁ツ
    2021-02-01 04:45

    "optimistic" approach for simple scenarios (demos)... dbContext.Find()'s intellisense help tells us that it either retrieves entity by key if already present in current context, or queries the database to get it... then we know if it exists to either add or update. i'm using EFCore v2.2.0.

      var existing = _context.Find(new object[] {item.ProductId});
      if (existing == null) _context.Add(item);
      else existing.Quantity = item.Quantity;
      _context.SaveChanges();
    

提交回复
热议问题