views:

34

answers:

2

I have a requirement to only save data to a table in a database (I don't need to read it)

If the record already exists I want to update it otherwise I will add it. It usually exists.

My entity context might already hold the object .. if it does I want to find it and use it again without causing it to refresh from the database when I 'find' it

i.e. The context holds a collection of entities (rows of a database) I want to find an entity in the collection and only want the context to go to the database if entity is not in the collection. I don't care about the current values of the entity .. I just want to update them.

Hope this is clear ..... thanks

+1  A: 

I may not be quite seeing the question, but I believe your looking for some sort of caching mechanism, I know for work we use devForces IdeaBlade which does the trick, however I believe you can create a simple caching mechanism custom to you needs.

http://blogs.msdn.com/b/jkowalski/archive/2009/06/11/tracing-and-caching-in-entity-framework-available-on-msdn-code-gallery.aspx

The bits on caching will be helpful, if this doesnt help tell me and I can dig a little deeper.

Gnostus
DevForce .. I am currently looking at that .. great product. But not the answer here.What I am saying is I just want to Update/Insert a row in a table. can I do this without causing a read each time.Secondly .. if I already have a collection of entities (rows in a table) how do I get the entity from the collection without the entityContext refreshing the entity from the database. I don't need to go to the database it is already in the collection. So I just want to find an entity in a collection.
Adam
+1  A: 

I believe you need to use GetObjectByKey() instead of using an ObjectQuery I believe an ObjectQuery always hits the backend datastore whatever it may be.

More Info here http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.getobjectbykey.aspx

Gratzy
Thanks .. there has to be a better way .. I think it has something to do with ObjectSet / ObjectQuery.This is a good site.http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-13-how-to-attach-an-entity-the-easy-way.aspxhttp://blogs.msdn.com/b/alexj/archive/2009/03/26/index-of-tips.aspx
Adam