1
I read several questions of the type, but I find my question very specific: How does the Entity framework "trackeia" the objects? That is, as when loading an object and even without sending the object back to the context class, just calling contexto.SaveChanges()
, it already automatically saves the changes? How is this object "followed" by the Entity framework? It is some kind of return by reference and such?
Even that part I know how it works, but I’ve seen codes like this:
m = contexto.FirstOrDefault(p => p.Id = id); m.Propriedade = "outro valor"; contexto.SaveChanges();
. In this case no Attach was called. There is also the following case:Debug.WriteLine(m.Id); contexto.Insert(m); contexto.SaveChanges(); Debug.WriteLine(m.Id)
in the first write the Id is null, whereas in the second the id was assigned without me doing it explicitly. I would like to know how this mechanism works. If when I call Insert it passes the reference of the object "m"– Raul Medeiros
@Raulmedeiros, that’s what I said in the answer... when you do m = context.Firstordefault(p => p.Id = id); m.Property = "other value"; context.Savechanges(); you go to the database and EF maps the properties with one state each property, when you call Savechanges() it knows which properties have been modified...
– Marco Souza
Already when you do Debug.Writeline(m.Id); context. Insert(m); context.Savechanges(); Debug.Writeline(m.Id) ... your "m" is certainly a context object, when you use Insert the context knows that that is a new object the id is surely generated with an auto-increment, behind the EF already reads the saved object in the database and maps all its properties ...
– Marco Souza