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
Entity framework 实体框架存储过程映射结果不是唯一实体_Entity Framework_Stored Procedures_Unique - Fatal编程技术网

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

Entity framework 实体框架存储过程映射结果不是唯一实体,entity-framework,stored-procedures,unique,Entity Framework,Stored Procedures,Unique,我遇到了一个问题,我映射了一个名为sp_getMyEntity的存储过程,它接受一个名为Id的参数,然后将结果映射到一个名为MyEntity的自定义实体 我用模拟数据来说明这一点。运行id为1的存储过程时,从数据库返回两个不同的结果: exec[dbo].[sp_getMyEntity]@Id=1 结果: ID-地址ID 1-6 1-3 当使用LINQ查询ObjectContext时,我得到了2个MyEntity的返回,但它们的AddressId都是6,而不是分别为6和3。以下是我使用LINQ的

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

我用模拟数据来说明这一点。运行id为1的存储过程时,从数据库返回两个不同的结果:

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

结果:

ID-地址ID

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不是唯一的吗?

因为SSDL标识为键的SP列不是唯一的列。不能有具有重复值的键。

因为SSDL标识为键的SP列不是唯一的列。您不能拥有具有重复值的密钥。

正如Craig在上文中所述,我的密钥属性不是唯一的,因此我最终更改了SQL以查看以下内容:

SELECT 
    ROW_NUMBER() OVER(ORDER BY Id) AS KeyId
  , Id
  , AddressId
FROM
  MyTable
WHERE
  Id = @Id
然后,EDMX xml如下所示,请注意,新键是bigint和Int64:


正如Craig在上面所说的,我的密钥属性不是唯一的,因此我最终更改了SQL以查看以下内容:

SELECT 
    ROW_NUMBER() OVER(ORDER BY Id) AS KeyId
  , Id
  , AddressId
FROM
  MyTable
WHERE
  Id = @Id
然后,EDMX xml如下所示,请注意,新键是bigint和Int64:


为什么这不会导致一个例外呢?我不知道;我不确定当SP返回重复密钥时,它应该如何运行。非常感谢您的帮助,我已经在下面完整地回答了这个问题。那么这不会导致异常吗?我不知道;我不确定SP返回重复密钥时它应该如何运行。非常感谢您的帮助,我已经完整地回答了下面的问题