imesh
imesh

Reputation: 1644

DbContext Generic Method to Find Key Members

Is there a way to find Key Members of an entity (TEntity) when using CodeFirst (DbContext) on a generic context similar to ObjectContext?

I can see how it's done using ObjectContext here.

Upvotes: 1

Views: 405

Answers (1)

imesh
imesh

Reputation: 1644

I'm sorry for the confusion, it seems like even with Code First approach we can find the key members on an entity using the object context as shown below:

var ObjectContext = ((IObjectContextAdapter)DbContext).ObjectContext;
var ObjectSet = ObjectContext .CreateObjectSet<TEntity>();
var EntitySet = ObjectSet.EntitySet;
var KeyMembers = EntitySet.ElementType.KeyMembers;

Upvotes: 1

Related Questions