1

We would like to have a method which copies all properties of a given input object to an existing entity framework core object which was loaded from the database and therefore is in attached state.

We have tried Automapper and AnyClone to copy all properties from the input to the database object. The copy itself works fine but when we then store/save the database object Entity Framework creates a new entry instead of updating the existing one.

Is there some kind of hidden property which we should not copy in order to not loose the attachment to the DbContext?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
marco birchler
  • 1,566
  • 2
  • 21
  • 45
  • Can you add the code you see the problem in which? It'll help diagnosing the problem better. It looks like that a property with the role of id/key is getting overridden. – Afshar Mohebi Sep 04 '19 at 15:45

2 Answers2

3

Entity Framework Core has this ability included:

db.Entry(current).CurrentValues.SetValues(ord);

Taken from Update Entity Framework object from another object of same type

infografnet
  • 3,749
  • 1
  • 35
  • 35
0

I think using NoTracking may help you. Here is a link to some explanation about what is NoTracking. https://www.c-sharpcorner.com/article/no-tracking-with-entity-framework-core/

Pedro Brito
  • 263
  • 1
  • 9