0

I'm trying to implement a CRUD using Winforms and Code First EF. For every instance of a Browser there's a Controller and for each Controller there's a Repository, and Repository is a class that inherits from DbContext. My Repository class has a method names GetNextPage(), which return a filtered List of my Entity.

I've been struggling to understand how caching is suppose to work, and what sense does it make at all. It took me a while to realise that by calling GetNextPage() a second time, although a query was being run against the database, the result set was still the same. Meaning, no updates in the records in the database where reflected on the collection returned by DbSet<>(). So that's when I realised DbContext was caching the content, but still running a query on the Database, which sounds pretty dumb.

The same is true if I try to DbGet<>().Single(), it runs a query against the database, but doesn't acctually update the entity. I was forced to detach the entity before so I can acctually get an updated object from the context.

And last but not least, after trying to delete an entity, if it fails because it contains dependant entities which are not deleted by cascade, if I try a DbGet<>().Single on the same entity, not before detaching it, the list of dependenty entities comes backup EMPTY!!! WTH??? This makes absolute no sense at all.

It's been really to understando EF at this point, specially because there's a lot of conflicting info about it on the internet, mainly because of the different version.

Well, I guess my question is, what's the point of caching?

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
Bruno Santos
  • 724
  • 7
  • 19
  • It sounds like something else might be wrong in your setup, can you share some of the code? – JanR May 13 '16 at 00:58
  • Also in regards to cascading deletes, you can configure this in your configuration, see this question:http://stackoverflow.com/questions/14898128/ef-code-first-cascade-delete-on-foreign-key-one-to-many , if you don't EF has a tendency to simply orphan the child objects. – JanR May 13 '16 at 00:59

0 Answers0