2010-03-02 110 views
1

我在那裏我已經映射命名爲sp_getMyEntity一個存儲過程,這需要在一個名爲標識一個參數,然後將結果映射到自定義實體稱爲myEntity所存在問題實體框架的存儲過程映射結果不是唯一的實體

我使用模擬數據來說明這一點。當運行具有1的ID的存儲過程,我從數據庫獲得兩個不同的結果返回:

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

結果:

ID - AddressId

1 - 6

1 - 3

使用LINQ查詢ObjectContext時,我得到2個MyEntity的後面,但AddressId都是t下襬分別是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所的不是唯一的?

回答

3

由於SSDL標識爲密鑰的SP列不是唯一列。您不能擁有重複值的「鑰匙」。

+0

如何走到這不會導致一個例外呢? – 2010-03-02 17:46:20

+0

我不知道;我不知道當SP返回重複密鑰時它應該如何處理。 – 2010-03-02 18:02:02

+0

非常感謝您的幫助,我在下面全部回答了這個問題 – dnoxs 2010-03-03 09:37:49

1

克雷格說我上面的主要財產是不是唯一的,所以我最終改變SQL來看看如下:

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

然後EDMX XML看起來如下,發現新的關鍵是BIGINTInt64的

<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> 
相關問題