27

I could not find an answer on the Internet.

Let's suppose I have a DbContext, and I just select all the entities from it. I don't add, update or delete any entity on the DbSet.

If I call SaveChanges afterwards on the DbSet. Does it actually waste resources establishing a connection and other stuff even If I did not made any changes to the DbSet?

Is it smart enough to detect if a change was made or not, and behave differently?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154
  • 2
    Have you tried it using a profiler to see? Seems this question is one that can be answered by a bit of testing. – Jamiec Nov 21 '14 at 13:00
  • 4
    No it doesn't, a DbContext tracks changes so it knows what to commit. If it has no knowledge of any changes then what exactly would be the point of connecting to the database. – Ben Robinson Nov 21 '14 at 13:05

1 Answers1

35

It uses EntityState to determine that there is nothing to commit and so does not waste resources.

http://msdn.microsoft.com/en-us/library/system.data.entitystate%28v=vs.110%29.aspx

Kaido
  • 3,383
  • 24
  • 34