Entity Framework 4 loads record by ID

So, I am considering creating a universal interface for interacting with my DataStorage from my objects, which I can change to use EF4 or SubSonic or NHibernate or some NoSQL option.

So, I have an ERD, and each table has an auto incrementing int column "TableNameID", which is the main key. I am trying to figure out how to get one record from a database using the primary key in a generic method

public T GetSingle<T>(int primaryKey) where T : class

How do you do this using EF4?

+3
source share
1 answer

ObjectContext.CreateObjectSet().

, - :

public T GetSingle<T>(int primaryKey) where T : class
{
    var q = Context.CreateObjectSet<T>().Where("it.TableNameID = @tableNameId");
    q.Parameters.Add(new ObjectParameter("tableNameId", primaryKey));
    return q.Single();
}
+2

Source: https://habr.com/ru/post/1748023/


All Articles