This question is locked. New answers and comments are not allowed.
Does anyone know of a way to create an object key generically for an entity from a list of primary key values? I want to implement a function like the following generically for any entity in a generic class, rather than having to implement is specifically for each class as I do now:
/// <summary>
/// Lookup an entity record by primary key values. Note that the entity is returned using
/// the cached read-only context but is not detached from the context so navigation properties
/// will work.
/// </summary>
/// <param name="keyValues">List of key values for the entity to delete</param>
/// <returns>Entity record if found, false if not</returns>
TEntity Find(params object[] keyValues);
This is identical to an API function that is implemented in the Entity Framework DbContext API, and it is a nice surface to work against in web applications where you need to look up entities by primary key values a lot, when the primary key values are passed around as part of a web request. I would like to be able to call GetObjectByKey() on my context to implement this, but to do that, I need to create an object key and right now the only way to do that is to create an instance of the entity and assign the primary keys to it.
I can easily do that in each specific repository type, because it is easy once I know the type of the entity, but is there some way to implement this in such a way that the object key can be created by matching the primary keys with those in the entity? In most cases there would only be one primary key, but some cases it might be a combination of fields.
I assume something like this could be done with reflection, or inspecting the metadata in the metadata container for the type of entity? But I am not sure where to start.
Any suggestions on how to go about implementing something like this?
/// <summary>
/// Lookup an entity record by primary key values. Note that the entity is returned using
/// the cached read-only context but is not detached from the context so navigation properties
/// will work.
/// </summary>
/// <param name="keyValues">List of key values for the entity to delete</param>
/// <returns>Entity record if found, false if not</returns>
TEntity Find(params object[] keyValues);
This is identical to an API function that is implemented in the Entity Framework DbContext API, and it is a nice surface to work against in web applications where you need to look up entities by primary key values a lot, when the primary key values are passed around as part of a web request. I would like to be able to call GetObjectByKey() on my context to implement this, but to do that, I need to create an object key and right now the only way to do that is to create an instance of the entity and assign the primary keys to it.
I can easily do that in each specific repository type, because it is easy once I know the type of the entity, but is there some way to implement this in such a way that the object key can be created by matching the primary keys with those in the entity? In most cases there would only be one primary key, but some cases it might be a combination of fields.
I assume something like this could be done with reflection, or inspecting the metadata in the metadata container for the type of entity? But I am not sure where to start.
Any suggestions on how to go about implementing something like this?