Home > C#

Entity Framework Tips and Tricks: Use GetObjectByKey When Querying a Single Entity

7. September 2011

I’ve been using Entity Framework a lot lately and it makes data access really nice. It’s fairly simple to use and maps all your tables to a .NET object model. However, just like any other library, it can be misused and have it’s own issues. I’ve been figuring some things out as I’ve been using it, so I thought I’d share a few tip and tricks that I’ve learned. Here’s the first one:

Use “GetObjectByKey” when Querying for a Single data entity

Take the following query:

var person = (from p in context.Persons
              where p.ID == id
              select p).FirstOrDefault();

This query will find all the “Person” entities that have a specific “ID” and then return the first one or null if it doesn’t exist. This query is perfectly valid and will operate as expected. However, if you were to call that query multiple times, it will hit the database every time it’s called and never cache the entity. In order to “cache” the entity for future calls you would need to keep the “person” variable around for any future use.

Now, take the following code:

var keyValues = new KeyValuePair<string, object>[] {
    new KeyValuePair<string, object>("ID", id)
};
var key = new EntityKey("DataEntities.Persons", keyValues);

var person = (Person)context.GetObjectByKey(key);

This code uses the “GetObjectByKey” method to return the same “Person” entity as the above query. However, there is one critical difference. This code will check the cache before hitting the database to see if the entity has already been loaded into memory using Entity Framework.

Basically, the first call to “GetObjectByKey” will query the database, and any subsequent calls for the same entity will just return it from the cache. You must remember when using this method that it wont refresh the data from the database if it exists in the cache, so in certain circumstances, it may still be better to perform a query to make sure the data returned is the latest and not stale.

The “GetObjectByKey” method will throw an ObjectNotFoundException if the requested object cannot be found. To avoid handling the exception, you can alternatively use the “TryGetObjectByKey” method instead.

Here’s a generic method and it’s sample usage that I’ve created to make using TryGetObjectKey a little simpler:

static T GetEntityByID<T>(string entitySetName, Guid id)
    where T: class
{
    object val = null;

    // Create the EntityKey
    var keyValues = new KeyValuePair<string, object>[] {
        new KeyValuePair<string, object>("ID", id)
    };
    var key = new EntityKey(
        string.Format("DataEntities.{0}", entitySetName),
        keyValues
    );

    // Try to get the Object using the Key
    context.TryGetObjectByKey(key, out val);

    // cast the object as T
    // this will return Null if the object doesn't exist
    return val as T;
}

// Example usage of the method
var person = GetEntityByID<Person>("Persons", id);

You could even go a step further and wrap the above “GetEntityByID<Person>()” call into a method like “GetPersonByID(Guid id)” to make it easier to use.

C# , , ,

Comments

9/8/2011 6:14:03 AM #
Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.
9/8/2011 2:26:40 PM #
Absolute accurate perceptions written in a very detailed manner on this page when it comes to gain some knowledge in regard to foreclosed properties. Foreclosures surrounded some of our states as economy hit people badly. Government should announce a workable plan to keep things in control.
9/8/2011 4:01:55 PM #
The only thing that is relevant is that Gears of war 3 kicks off in just over a week
9/9/2011 3:51:39 AM #
Thanks for the info. Good to take into account on my next trip!
9/9/2011 4:31:02 AM #
I have learned a lot from this. A good takeaway. Will keep it in mind for my next trip! Cheers.
9/9/2011 3:55:13 PM #
Rad, that was a very good post. Even so I'm experiencing issues with your main Feed. I don't fully understand why but I am unable to subscribe to it. Perhaps there is someone receiving related issues? Anybody who understands generously reply. Regards. Soo Yun
9/9/2011 5:59:58 PM #
Exceptional post however , I was wondering if you could write a litte more on this subject? I'd be very grateful if you could elaborate a little bit further. Thank you!
9/9/2011 11:34:57 PM #
I understand something much more hard on different blogs each day. It'll always get inspiring to read written content from other internet writers and practice a little something from their store. I’d prefer to work with some of your respective written content on my personal site if you don’t mind. Needless to say I’ll offer you a link on my blog. Nice one for posting.