Like the Entity Framework Trackeia objects?

Asked

Viewed 44 times

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?

1 answer

1

When you read a database object the Entity Framework maps property by property and it sets a state (States) for each property, so he knows any changes that occur with the object.

There is still the proxy, This proxy overrides some virtual entity properties to insert hooks to perform actions automatically when the property is accessed.

Any change that is made to the object without the Entity Framework knowing, should do the attach of the object so that the Entity Framework can map it.

  • 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"

  • @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...

  • 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 ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.