DataServieException:Oracle、EFOracleProvider、POCO 和 EF 4.0

发布于 2024-09-07 00:38:31 字数 6599 浏览 6 评论 0原文

我尝试将 EF 4.0 的新 POCO 功能与 EFOracleProvider 结合使用。我已重新编译 EFOracleProvider(使用 ODAC 而不是 System.Data.OracleClient)以针对 .NET Framework 4(并将其放入 4.0 GAC 中)。到目前为止一切都很好。

我在 WCF 数据服务中托管实体模型:

class DivaDispoDataService : DataService<DivaDispoContainer>
{
    public static void InitializeService(IDataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
    }
}

我的 EDMX 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl"  xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="DivaDispo.Store" Alias="Self" Provider="EFOracleProvider" ProviderManifestToken="10g">
        <EntityContainer Name="DivaDispoStoreContainer">
          <EntitySet Name="DIW_USER" EntityType="DivaDispo.Store.DIW_USER" store:Type="Tables" Schema="" />
        </EntityContainer>
        <EntityType Name="DIW_USER">
          <Key>
            <PropertyRef Name="IDX" />
          </Key>
          <Property Name="IDX" Type="number" Nullable="false" Precision="10" />
          <Property Name="USERNAME" Type="number" Precision="10" />
          <Property Name="PERSONNELNUMBER" Type="number" Precision="10" />
          <Property Name="PASSWORD" Type="varchar2" MaxLength="10" />
          <Property Name="ACTIVATED" Type="number" Precision="1" />
          <Property Name="ROLE" Type="number" Precision="1" />
        </EntityType>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm"  xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="DivaDispo.Model" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
        <EntityContainer Name="DivaDispoContainer" annotation:LazyLoadingEnabled="false">
          <EntitySet Name="User" EntityType="DivaDispo.Model.User" />
        </EntityContainer>
        <EntityType Name="User">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <Property Name="ID" Type="Decimal" Nullable="false" Precision="10" Scale="0"    />
          <Property Name="Username" Type="Decimal" Precision="10" Scale="0" />
          <Property Name="PersonnelNumber" Type="Decimal" Precision="10" Scale="0" />
          <Property Name="Password" Type="String" MaxLength="10" Unicode="false"  FixedLength="false" />
          <Property Name="Activated" Type="Boolean" />
          <Property Name="Role" Type="Boolean" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs" Space="C-S">
        <EntityContainerMapping StorageEntityContainer="DivaDispoStoreContainer" CdmEntityContainer="DivaDispoContainer">
          <EntitySetMapping Name="User">
            <EntityTypeMapping TypeName="IsTypeOf(DivaDispo.Model.User)">
              <MappingFragment StoreEntitySet="DIW_USER">
                <ScalarProperty Name="ID" ColumnName="IDX" />
                <ScalarProperty Name="Username" ColumnName="USERNAME" />
                <ScalarProperty Name="PersonnelNumber" ColumnName="PERSONNELNUMBER" />
                <ScalarProperty Name="Password" ColumnName="PASSWORD" />
                <ScalarProperty Name="Activated" ColumnName="ACTIVATED" />
                <ScalarProperty Name="Role" ColumnName="ROLE" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
</edmx:Edmx>

我的 POCO 类如下所示:

public partial class User
{
    public virtual decimal ID { get; set; }
    public virtual Nullable<decimal> Username { get; set; }
    public virtual Nullable<decimal> PersonnelNumber { get; set; }
    public virtual string Password { get; set; }
    public virtual Nullable<bool> Activated { get; set; }
    public virtual Nullable<bool> Role { get; set; }
 }

并且 DataContext 如下所示:

public partial class DivaDispoContainer : ObjectContext
{
    public const string ConnectionString = "name=DivaDispoContainer";
    public const string ContainerName = "DivaDispoContainer";

    #region Constructors

    public DivaDispoContainer()
        : base(ConnectionString, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = false;
    }

    public DivaDispoContainer(string connectionString)
        : base(connectionString, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = false;
    }

    public DivaDispoContainer(EntityConnection connection)
        : base(connection, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = false;
    }

    #endregion

    #region ObjectSet Properties

    public ObjectSet<User> User
    {
        get { return _user  ?? (_user = CreateObjectSet<User>("User")); }
    }
    private ObjectSet<User> _user;
  }

POCO 类和 DataContext 是使用 Visual Studio 2010 中的 POCO 模板生成的

。启动我的 WCF 服务并想要查询用户,我收到一个 System.Data.Services.DataServiceException ,上面写着 somtihng 就像

500: Internal Server error. The type 'System.Data.Entity.DynamicProxies.User_057822846B2B8DD7BB03058490B27D19E6C634EACF33438FE886  19C8BBB1CF74' is not a komplex type or entity type. 

当我查看 dubgger 时,我可以看到这些值已从数据库中读取(因此我认为 EFOracleProvider 工作正常)并且 DynamicProxies.User_.... 是从我的 User 类派生的(其中包含来自数据库的数据)。所以问题是:为什么我会收到此异常?有谁知道那里发生了什么事吗? DynamicProxies.User_.... 自动生成的类是做什么用的?异常在 WebUtil 类的方法 GetNonPrimitiveResourceType 中抛出。或者也许我忽略了一些事情?

任何帮助都非常感激......

I try to use the new POCO capabilities of EF 4.0 in combination with the EFOracleProvider. I have recompiled the EFOracleProvider (using ODAC instead of System.Data.OracleClient) to target the .NET Framework 4 (und put it in the 4.0 GAC). Everything ok so far.

I host the Entity Model in a WCF Data Service:

class DivaDispoDataService : DataService<DivaDispoContainer>
{
    public static void InitializeService(IDataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
    }
}

My EDMX File looks like this:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl"  xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="DivaDispo.Store" Alias="Self" Provider="EFOracleProvider" ProviderManifestToken="10g">
        <EntityContainer Name="DivaDispoStoreContainer">
          <EntitySet Name="DIW_USER" EntityType="DivaDispo.Store.DIW_USER" store:Type="Tables" Schema="" />
        </EntityContainer>
        <EntityType Name="DIW_USER">
          <Key>
            <PropertyRef Name="IDX" />
          </Key>
          <Property Name="IDX" Type="number" Nullable="false" Precision="10" />
          <Property Name="USERNAME" Type="number" Precision="10" />
          <Property Name="PERSONNELNUMBER" Type="number" Precision="10" />
          <Property Name="PASSWORD" Type="varchar2" MaxLength="10" />
          <Property Name="ACTIVATED" Type="number" Precision="1" />
          <Property Name="ROLE" Type="number" Precision="1" />
        </EntityType>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm"  xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="DivaDispo.Model" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
        <EntityContainer Name="DivaDispoContainer" annotation:LazyLoadingEnabled="false">
          <EntitySet Name="User" EntityType="DivaDispo.Model.User" />
        </EntityContainer>
        <EntityType Name="User">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <Property Name="ID" Type="Decimal" Nullable="false" Precision="10" Scale="0"    />
          <Property Name="Username" Type="Decimal" Precision="10" Scale="0" />
          <Property Name="PersonnelNumber" Type="Decimal" Precision="10" Scale="0" />
          <Property Name="Password" Type="String" MaxLength="10" Unicode="false"  FixedLength="false" />
          <Property Name="Activated" Type="Boolean" />
          <Property Name="Role" Type="Boolean" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs" Space="C-S">
        <EntityContainerMapping StorageEntityContainer="DivaDispoStoreContainer" CdmEntityContainer="DivaDispoContainer">
          <EntitySetMapping Name="User">
            <EntityTypeMapping TypeName="IsTypeOf(DivaDispo.Model.User)">
              <MappingFragment StoreEntitySet="DIW_USER">
                <ScalarProperty Name="ID" ColumnName="IDX" />
                <ScalarProperty Name="Username" ColumnName="USERNAME" />
                <ScalarProperty Name="PersonnelNumber" ColumnName="PERSONNELNUMBER" />
                <ScalarProperty Name="Password" ColumnName="PASSWORD" />
                <ScalarProperty Name="Activated" ColumnName="ACTIVATED" />
                <ScalarProperty Name="Role" ColumnName="ROLE" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
</edmx:Edmx>

My POCO Class looks like this:

public partial class User
{
    public virtual decimal ID { get; set; }
    public virtual Nullable<decimal> Username { get; set; }
    public virtual Nullable<decimal> PersonnelNumber { get; set; }
    public virtual string Password { get; set; }
    public virtual Nullable<bool> Activated { get; set; }
    public virtual Nullable<bool> Role { get; set; }
 }

and the DataContext like this:

public partial class DivaDispoContainer : ObjectContext
{
    public const string ConnectionString = "name=DivaDispoContainer";
    public const string ContainerName = "DivaDispoContainer";

    #region Constructors

    public DivaDispoContainer()
        : base(ConnectionString, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = false;
    }

    public DivaDispoContainer(string connectionString)
        : base(connectionString, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = false;
    }

    public DivaDispoContainer(EntityConnection connection)
        : base(connection, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = false;
    }

    #endregion

    #region ObjectSet Properties

    public ObjectSet<User> User
    {
        get { return _user  ?? (_user = CreateObjectSet<User>("User")); }
    }
    private ObjectSet<User> _user;
  }

The POCO classes and the DataContext are generated with the POCO Template from Visual Studio 2010.

When I start my WCF Service and want to query the users I receive an System.Data.Services.DataServiceException which says somtihng like

500: Internal Server error. The type 'System.Data.Entity.DynamicProxies.User_057822846B2B8DD7BB03058490B27D19E6C634EACF33438FE886  19C8BBB1CF74' is not a komplex type or entity type. 

When I look in the dubgger I can see that the values have been read from the database (therefore I think the EFOracleProvider works ok) and that the DynamicProxies.User_.... is derrived from my User class (which contains at that point the data from the database). So the question is: Why do I receive this exception? Does anyone know whats going on there? What's the DynamicProxies.User_.... automaic generated class for? The Exception is thrown in the Method GetNonPrimitiveResourceType of the class WebUtil. Or maybe I have something overlooked?

Any help greatly appreciated....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2024-09-14 00:38:31

Craig 到博客文章的链接 http://msdn.microsoft.com/ en-us/library/ee705457.aspx 揭示了答案。它在那里指出了这一点

The POCO proxy type cannot be directly serialized or deserialized by WCF 

,这正是我的问题。如果我从生成的 POCO 类的属性中删除虚拟,则运行时不会生成代理类型,异常也会消失。

再次感谢您的链接,克雷格。

The link from Craig to the blog post http://msdn.microsoft.com/en-us/library/ee705457.aspx revealed the answer. It states there that

The POCO proxy type cannot be directly serialized or deserialized by WCF 

and that is exactly my problem. If I remove the virtual from the properties of the genrated POCO class the runtime doesnt generate the proxy types and the exception dissapears.

Thanks again for the link, Craig.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文