Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework 6 data context from wcf data service

I used to access the data context of my (ef 5.0) entities from inside a wcf data services service operation with this.CurrentDataSource.MyEntity. My data service inherited from DataService<T>. Now I wanted to use entity framework 6.0 and read on the internet, I should inherit the service from EntityFrameworkDataService<T>. But now from inside my service operations, I cannot access my data context anymore. this.CurrentDataSource doesn't contain any reference to the entities.

like image 966
Matthias Avatar asked Jan 11 '23 22:01

Matthias


1 Answers

Here's my workaround using an extension method that gets the underlying data model via (cached) reflection info. It works for the current Microsoft.OData.EntityFrameworkProvider version 1.0.0 alpha 2.

Sample usage is 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
{
    /// <summary>
    /// Gets the underlying data model currently used by an EntityFrameworkDataServiceProvider2.
    /// </summary>
    /// <remarks>
    /// TODO: Obsolete this method if the API changes to support access to the model.
    /// Reflection is used as a workaround because EntityFrameworkDataServiceProvider2 doesn't (yet) provide access to its underlying data source. 
    /// </remarks>
    public static T GetDataModel<T>(this EntityFrameworkDataServiceProvider2<T> efProvider) where T : class
    {
        if (efProvider != null)
        {
            Type modelType = typeof(T);

            // Get the innerProvider field info for an EntityFrameworkDataServiceProvider2 of the requested type
            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)
            {
                // Get the CurrentDataSource property of the innerProvider
                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 suggestion taken from Caching reflection data

like image 93
Dave A-W Avatar answered Feb 02 '23 14:02

Dave A-W