i think that below codes is very useful. But i try to create EntityKEy. i used:
EntityKey key = new EntityKey(entitySetKEYName, "XTableId", id);
But i dont want to use this. my table PRIMARYID YtableId,ZTableId, etc… How can i write this:
EntityKey key = new EntityKey(entitySetKEYName, Find().PrimaryKEY, id);
public T GetByPrimaryKey<T>(int id)
{
string entitySetName = _context.MetadataWorkspace.GetEntityContainer(_context.DefaultContainerName, DataSpace.CSpace).BaseEntitySets.
Where(q => q.ElementType.Name == typeof(T).Name).FirstOrDefault().Name;
string entitySetKEYName = string.Format("{0}.{1}", _context.DefaultContainerName, entitySetName);
EntityKey key = new EntityKey(entitySetKEYName, "XTableId", id);
return (T)_context.GetObjectByKey(key);
}
HOW CAN AUTOMATICALLY DETECT PRIMARY KEY????????????????
First,PK is not necessary Int32, it can be Guid or even multi property complex key, so generic method should except Object as PK…
Here is my pretty ugly variant, but it does the job, though it need proper testing.