Use GetObjectByKey() for better performance

When I want to fetch an entity, I normally have a method that uses a query.

image

This method will always query the database and you will have an up-to-date entity.

 

But if you need better performance perhaps GetObjectByKey() is a better option.

This method will check the cache first and returns the entity when it has been loaded already.

When it’s not in the cache, it would still query the database.

 

You need to supply an EntityKey(string qualifiedEntitySetName, string keyName, object keyValue)

I’ve made a generic method which automatically determines the entitySetName:

image

here’s an example of a call:

image

 

ps: I have hardcoded the keyName, because all my tables have an “ID” primary key.

When this is not the case, check out the CreateEntityKey method

posted on 2010-09-06 12:37  xqiwei  阅读(512)  评论(0编辑  收藏  举报