Here my workaround is using an extension method that gets the underlying data model through the (cached) reflection information. It works for the current version of Microsoft.OData.EntityFrameworkProvider version 1.0.0 alpha 2.
Using the example for a custom WebGet method:
[WebGet]
public string GetMyEntityName(string myEntityKey)
{
var model = this.CurrentDataSource.GetDataModel();
var entity = model.MyEntity.Find(myEntityKey);
return entity.Name;
}
And implementation:
public static class EntityFrameworkDataServiceProvider2Extensions
{
public static T GetDataModel<T>(this EntityFrameworkDataServiceProvider2<T> efProvider) where T : class
{
if (efProvider != null)
{
Type modelType = typeof(T);
FieldInfo ipField;
if (!InnerProviderFieldInfoCache.TryGetValue(modelType, out ipField))
{
ipField = efProvider.GetType().GetField("innerProvider", BindingFlags.NonPublic | BindingFlags.Instance);
InnerProviderFieldInfoCache.Add(modelType, ipField);
}
var innerProvider = ipField.GetValue(efProvider);
if (innerProvider != null)
{
PropertyInfo cdsProperty;
if (!CurrentDataSourcePropertyInfoCache.TryGetValue(modelType, out cdsProperty))
{
cdsProperty = innerProvider.GetType().GetProperty("CurrentDataSource");
CurrentDataSourcePropertyInfoCache.Add(modelType, cdsProperty);
}
return cdsProperty.GetValue(innerProvider, null) as T;
}
}
return null;
}
private static readonly ConditionalWeakTable<Type, FieldInfo> InnerProviderFieldInfoCache = new ConditionalWeakTable<Type, FieldInfo>();
private static readonly ConditionalWeakTable<Type, PropertyInfo> CurrentDataSourcePropertyInfoCache = new ConditionalWeakTable<Type, PropertyInfo>();
}
System.Runtime.CompilerServices.ConditionalWeakTable was used to cache reflection results based on a sentence taken from Reflection Caching Data
source
share