实体框架存储过程映射结果不是唯一实体

时间:2010-03-02 16:49:56

标签: entity-framework stored-procedures unique

我遇到一个问题,我已经映射了一个名为 sp_getMyEntity 的存储过程,它接受一个名为 Id 的参数,然后将结果映射到一个名为<的自定义实体强> myEntity所

我正在使用模拟数据来说明这一点。当我运行id为1的存储过程时,我从数据库中得到两个不同的结果:

exec [dbo].[sp_getMyEntity] @Id=1

结果:

ID - AddressId

1 - 6

1 - 3

当使用LINQ查询ObjectContext时,我得到了2个MyEntity,但是它们的AddressId分别为6,而不是6和3。这是我使用LINQ的呼叫:

Entities context = new Entities();
var entities = from s in context.GetMyEntity(1)
               select s;
return entities.ToList();

这是EDMX xml:

<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
    <Schema Namespace="MyProjectModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
      <EntityContainer Name="MyProjectModelStoreContainer">
        <EntitySet Name="MyEntitySet" EntityType="MyProjectModel.Store.MyEntity" />
      </EntityContainer>
      <Function Name="sp_getMyEntity" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
        <Parameter Name="Id" Type="int" Mode="In" />
      </Function>
      <EntityType Name="MyEntity">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="int" Nullable="false" />
        <Property Name="AddressId" Type="int" Nullable="true" />
      </EntityType>     
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="MyProjectModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
        <EntityContainer Name="MyProjectViewEntities" >
          <EntitySet Name="MyEntitySet" EntityType="MyProjectModel.MyEntity" />
          <FunctionImport Name="GetMyEntity" EntitySet="MyEntitySet" ReturnType="Collection(MyProjectModel.MyEntity)">
            <Parameter Name="Id" Mode="In" Type="Int32" />
          </FunctionImport>
        </EntityContainer>
        <EntityType Name="MyEntity">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Int32" Nullable="false" />
          <Property Name="AddressId" Type="Int32" Nullable="true" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
        <EntityContainerMapping StorageEntityContainer="MyProjectModelStoreContainer" CdmEntityContainer="MyProjectViewEntities" >
          <FunctionImportMapping FunctionImportName="GetMyEntity" FunctionName="MyProjectModel.Store.sp_getMyEntity" />
          <EntitySetMapping Name="MyEntitySet">
            <EntityTypeMapping TypeName="IsTypeOf(MyProjectModel.MyEntity)">
              <MappingFragment StoreEntitySet="MyEntitySet">
                <ScalarProperty Name="AddressId" ColumnName="AddressId" />
                <ScalarProperty Name="Id" ColumnName="Id" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
    <edmx:Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </edmx:Connection>
    <edmx:Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
      </DesignerInfoPropertySet>
    </edmx:Options>
    <!-- Diagram content (shape and connector positions) -->
    <edmx:Diagrams>
      <Diagram Name="MyProjectModel" ZoomLevel="100" >
        <EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.25" PointY="0.5" Height="7.8375048828125" />
        <EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.5" PointY="1.375" Height="1.2636116536458335" /></Diagram></edmx:Diagrams>
  </edmx:Designer>
</edmx:Edmx>

为什么MyEntity不是唯一的任何想法?

2 个答案:

答案 0 :(得分:3)

因为SSDL识别为密钥的SP列不是唯一列。您不能拥有重复值的“密钥”。

答案 1 :(得分:1)

正如Craig在上面所述,我的Key属性并不是唯一的,所以我最终更改了SQL以查看以下内容:

SELECT 
    ROW_NUMBER() OVER(ORDER BY Id) AS KeyId
  , Id
  , AddressId
FROM
  MyTable
WHERE
  Id = @Id

然后EDMX xml如下所示,注意新键是 bigint Int64

<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
    <Schema Namespace="MyProjectModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
      <EntityContainer Name="MyProjectModelStoreContainer">
        <EntitySet Name="MyEntitySet" EntityType="MyProjectModel.Store.MyEntity" />
      </EntityContainer>
      <Function Name="sp_getMyEntity" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo">
        <Parameter Name="Id" Type="int" Mode="In" />
      </Function>
      <EntityType Name="MyEntity">
        <Key>
          <PropertyRef Name="KeyId" />
        </Key>
        <Property Name="KeyId" Type="bigint" Nullable="false" />
        <Property Name="Id" Type="int" Nullable="false" />
        <Property Name="AddressId" Type="int" Nullable="true" />
      </EntityType>     
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="MyProjectModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
        <EntityContainer Name="MyProjectViewEntities" >
          <EntitySet Name="MyEntitySet" EntityType="MyProjectModel.MyEntity" />
          <FunctionImport Name="GetMyEntity" EntitySet="MyEntitySet" ReturnType="Collection(MyProjectModel.MyEntity)">
            <Parameter Name="Id" Mode="In" Type="Int32" />
          </FunctionImport>
        </EntityContainer>
        <EntityType Name="MyEntity">
          <Key>
            <PropertyRef Name="KeyId" />
          </Key>
          <Property Name="KeyId" Type="Int64" Nullable="false" />
          <Property Name="Id" Type="Int32" Nullable="false" />
          <Property Name="AddressId" Type="Int32" Nullable="true" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
        <EntityContainerMapping StorageEntityContainer="MyProjectModelStoreContainer" CdmEntityContainer="MyProjectViewEntities" >
          <FunctionImportMapping FunctionImportName="GetMyEntity" FunctionName="MyProjectModel.Store.sp_getMyEntity" />
          <EntitySetMapping Name="MyEntitySet">
            <EntityTypeMapping TypeName="IsTypeOf(MyProjectModel.MyEntity)">
              <MappingFragment StoreEntitySet="MyEntitySet">
                <ScalarProperty Name="AddressId" ColumnName="AddressId" />
                <ScalarProperty Name="Id" ColumnName="Id" />
                <ScalarProperty Name="KeyId" ColumnName="KeyId" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
    <edmx:Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </edmx:Connection>
    <edmx:Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
      </DesignerInfoPropertySet>
    </edmx:Options>
    <!-- Diagram content (shape and connector positions) -->
    <edmx:Diagrams>
      <Diagram Name="MyProjectModel" ZoomLevel="100" >
        <EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.25" PointY="0.5" Height="7.8375048828125" />
        <EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.5" PointY="1.375" Height="1.2636116536458335" /></Diagram></edmx:Diagrams>
  </edmx:Designer>
</edmx:Edmx>