Skip to content

Instantly share code, notes, and snippets.

@ro31337
Created August 22, 2014 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ro31337/71c44737f6d3e6450b87 to your computer and use it in GitHub Desktop.
Save ro31337/71c44737f6d3e6450b87 to your computer and use it in GitHub Desktop.
public partial class MyContext
{
private static Dictionary<Type, EntitySetBase> _mappingCache =
new Dictionary<Type, EntitySetBase>();
private string GetTableName(Type type)
{
EntitySetBase es = GetEntitySet(type);
return string.Format("[{0}].[{1}]",
es.MetadataProperties["Schema"].Value,
es.MetadataProperties["Table"].Value);
}
private string GetPrimaryKeyName(Type type)
{
EntitySetBase es = GetEntitySet(type);
return es.ElementType.KeyMembers[0].Name;
}
private EntitySetBase GetEntitySet(Type type)
{
if (!_mappingCache.ContainsKey(type))
{
ObjectContext octx = ((IObjectContextAdapter)this).ObjectContext;
string typeName = ObjectContext.GetObjectType(type).Name;
var es = octx.MetadataWorkspace
.GetItemCollection(DataSpace.SSpace)
.GetItems<EntityContainer>()
.SelectMany(c => c.BaseEntitySets
.Where(e => e.Name == typeName))
.FirstOrDefault();
if (es == null)
throw new ArgumentException("Entity type not found in GetTableName", typeName);
_mappingCache.Add(type, es);
}
return _mappingCache[type];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment