Created
August 22, 2014 12:23
-
-
Save ro31337/71c44737f6d3e6450b87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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