Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自wcf数据服务的实体框架6数据上下文_C#_Entity Framework_Wcf Data Services_Objectcontext - Fatal编程技术网

C# 来自wcf数据服务的实体框架6数据上下文

C# 来自wcf数据服务的实体框架6数据上下文,c#,entity-framework,wcf-data-services,objectcontext,C#,Entity Framework,Wcf Data Services,Objectcontext,我曾经使用this.CurrentDataSource.MyEntity从wcf数据服务操作中访问我的(ef 5.0)实体的数据上下文。 我的数据服务继承自DataService。现在我想使用entity framework 6.0并在internet上阅读,我应该从EntityFrameworkDataService继承服务。但现在,从我的服务操作内部,我无法再访问我的数据上下文此。CurrentDataSource不包含对实体的任何引用。似乎EntitiyFrameworkDataServi

我曾经使用
this.CurrentDataSource.MyEntity
从wcf数据服务操作中访问我的(ef 5.0)实体的数据上下文。
我的数据服务继承自
DataService
。现在我想使用entity framework 6.0并在internet上阅读,我应该从
EntityFrameworkDataService
继承服务。但现在,从我的服务操作内部,我无法再访问我的数据上下文
此。CurrentDataSource
不包含对实体的任何引用。

似乎
EntitiyFrameworkDataService
不提供对底层
数据服务及其当前数据源的访问,如上所述和所述。

因此,唯一的方法似乎是每次更改每个服务操作以创建新的数据上下文。

下面是我的解决方法,使用扩展方法通过(缓存的)反射信息获取基础数据模型。它适用于当前的Microsoft.OData.EntityFrameworkProvider版本1.0.0 alpha 2

示例用法适用于自定义WebGet方法:

    [WebGet]
    public string GetMyEntityName(string myEntityKey)
    {
        var model = this.CurrentDataSource.GetDataModel();
        var entity = model.MyEntity.Find(myEntityKey);
        return entity.Name;
    }
和执行:

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>();
}
公共静态类EntityFrameworkDataServiceProvider2Extensions
{
/// 
///获取EntityFrameworkDataServiceProvider2当前使用的基础数据模型。
/// 
/// 
///TODO:如果API更改以支持对模型的访问,则此方法将被废弃。
///反射用作解决方法,因为EntityFrameworkDataServiceProvider2(尚未)提供对其底层数据源的访问。
/// 
公共静态T GetDataModel(此EntityFrameworkDataServiceProvider2 efProvider),其中T:class
{
if(efProvider!=null)
{
类型modelType=typeof(T);
//获取请求类型的EntityFrameworkDataServiceProvider2的innerProvider字段信息
FieldInfo ipField;
如果(!InnerProviderFieldInfoCache.TryGetValue(modelType,out ipField))
{
ipField=efProvider.GetType().GetField(“innerProvider”,BindingFlags.NonPublic | BindingFlags.Instance);
InnerProviderFieldInfoCache.Add(modelType,ipField);
}
var innerProvider=ipField.GetValue(efProvider);
如果(innerProvider!=null)
{
//获取innerProvider的CurrentDataSource属性
不动产信息CDS不动产;
if(!CurrentDataSourcePropertyInFoccache.TryGetValue(modelType,out CDSPProperty))
{
cdsProperty=innerProvider.GetType().GetProperty(“CurrentDataSource”);
CurrentDataSourcePropertyInfoCache.Add(模型类型,CDSPProperty);
}
将cdsProperty.GetValue(innerProvider,null)返回为T;
}
}
返回null;
}
私有静态只读条件WeakTable InnerProviderFieldInfoCache=new ConditionalWeakTable();
私有静态只读条件weaktable CurrentDataSourcePropertyInFoccache=new ConditionalWeakTable();
}

被用来缓存反射结果,这是基于一个来自它的建议!下面是在VB.NET中转换的代码

Imports System.Reflection
Imports System.Runtime.CompilerServices
Imports System.Data.Services.Providers

Public Module 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>
    <System.Runtime.CompilerServices.Extension> _
    Public Function GetDataModel(Of T As Class)(efProvider As EntityFrameworkDataServiceProvider2(Of T)) As T
        If efProvider IsNot Nothing Then
            Dim modelType As Type = GetType(T)

            ' Get the innerProvider field info for an EntityFrameworkDataServiceProvider2 of the requested type
            Dim ipField As FieldInfo = Nothing
            If Not InnerProviderFieldInfoCache.TryGetValue(modelType, ipField) Then
                ipField = efProvider.[GetType]().GetField("innerProvider", BindingFlags.NonPublic Or BindingFlags.Instance)
                InnerProviderFieldInfoCache.Add(modelType, ipField)
            End If

            Dim innerProvider = ipField.GetValue(efProvider)
            If innerProvider IsNot Nothing Then
                ' Get the CurrentDataSource property of the innerProvider
                Dim cdsProperty As PropertyInfo = Nothing
                If Not CurrentDataSourcePropertyInfoCache.TryGetValue(modelType, cdsProperty) Then
                    cdsProperty = innerProvider.[GetType]().GetProperty("CurrentDataSource")
                    CurrentDataSourcePropertyInfoCache.Add(modelType, cdsProperty)
                End If
                Return TryCast(cdsProperty.GetValue(innerProvider, Nothing), T)
            End If
        End If
        Return Nothing
    End Function

    Private ReadOnly InnerProviderFieldInfoCache As New ConditionalWeakTable(Of Type, FieldInfo)()
    Private ReadOnly CurrentDataSourcePropertyInfoCache As New ConditionalWeakTable(Of Type, PropertyInfo)()
End Module
导入系统反射
导入System.Runtime.CompilerServices
导入System.Data.Services.Providers
公共模块EntityFrameworkDataServiceProvider2Extensions
''' 
''获取EntityFrameworkDataServiceProvider2当前使用的基础数据模型。
''' 
''' 
''TODO:如果API更改以支持对模型的访问,则废弃此方法。
''反射被用作解决方法,因为EntityFrameworkDataServiceProvider2(尚未)提供对其底层数据源的访问。
''' 
_
公共函数GetDataModel(属于T类)(efProvider作为EntityFrameworkDataServiceProvider2(属于T类))作为T
如果efProvider不是空的,那么
Dim modelType As Type=GetType(T)
'获取请求类型的EntityFrameworkDataServiceProvider2的innerProvider字段信息
Dim ipField As FieldInfo=无
如果不是InnerProviderFieldInfoCache.TryGetValue(modelType,ipField),则
ipField=efProvider。[GetType]().GetField(“innerProvider”,BindingFlags.NonPublic或BindingFlags.Instance)
InnerProviderFieldInfoCache.Add(模型类型,ipField)
如果结束
Dim innerProvider=ipField.GetValue(efProvider)
如果innerProvider不是空的,那么
'获取innerProvider的CurrentDataSource属性
Dim CDS属性为PropertyInfo=Nothing
如果不是CurrentDataSourcePropertyInFoccache.TryGetValue(modelType,CDSPProperty),则
cdsProperty=innerProvider。[GetType]().GetProperty(“CurrentDataSource”)
CurrentDataSourcePropertyInfoCache.Add(模型类型,CDSPProperty)
如果结束
返回TryCast(cdsProperty.GetValue(innerProvider,Nothing),T)
如果结束
如果结束
一无所获
端函数
私有只读InnerProviderFieldInfoCache作为新的ConditionalWeakTable(类型为FieldInfo)()
Private ReadOnly CurrentDataSourcePropertyInFoccache作为新的ConditionalWeakTable(类型为PropertyInfo)()
端模块

注意:Microsoft.OData.EntityFrameworkProvider 1.0.0 beta 2版不再需要此功能。CurrentDataSource现在返回正确类型的模型。