Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity framework stored procedure mapping results not unique entities
    primarykey
    data
    text
    <p>I have a problem where I've mapped a stored procedure named <strong>sp_getMyEntity</strong>, which takes in one parameter called <strong>Id</strong> and then maps the results to a custom entity called <strong>MyEntity</strong></p> <p>I'm using mock data to illustrate the point. When I run the stored procedure with an id of 1, I get two distinct results back from the database:</p> <p><code>exec [dbo].[sp_getMyEntity] @Id=1</code></p> <p>Results:</p> <p>ID - AddressId</p> <p>1 - 6</p> <p>1 - 3</p> <p>When querying the ObjectContext using LINQ, I get 2 MyEntity's back but the AddressId for both of them is 6, not 6 and 3 respectively. Here is my call using LINQ:</p> <pre><code>Entities context = new Entities(); var entities = from s in context.GetMyEntity(1) select s; return entities.ToList(); </code></pre> <p>Here is the EDMX xml:</p> <pre><code>&lt;edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"&gt; &lt;!-- EF Runtime content --&gt; &lt;edmx:Runtime&gt; &lt;!-- SSDL content --&gt; &lt;edmx:StorageModels&gt; &lt;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"&gt; &lt;EntityContainer Name="MyProjectModelStoreContainer"&gt; &lt;EntitySet Name="MyEntitySet" EntityType="MyProjectModel.Store.MyEntity" /&gt; &lt;/EntityContainer&gt; &lt;Function Name="sp_getMyEntity" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="false" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo"&gt; &lt;Parameter Name="Id" Type="int" Mode="In" /&gt; &lt;/Function&gt; &lt;EntityType Name="MyEntity"&gt; &lt;Key&gt; &lt;PropertyRef Name="Id" /&gt; &lt;/Key&gt; &lt;Property Name="Id" Type="int" Nullable="false" /&gt; &lt;Property Name="AddressId" Type="int" Nullable="true" /&gt; &lt;/EntityType&gt; &lt;/Schema&gt; &lt;/edmx:StorageModels&gt; &lt;!-- CSDL content --&gt; &lt;edmx:ConceptualModels&gt; &lt;Schema Namespace="MyProjectModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm"&gt; &lt;EntityContainer Name="MyProjectViewEntities" &gt; &lt;EntitySet Name="MyEntitySet" EntityType="MyProjectModel.MyEntity" /&gt; &lt;FunctionImport Name="GetMyEntity" EntitySet="MyEntitySet" ReturnType="Collection(MyProjectModel.MyEntity)"&gt; &lt;Parameter Name="Id" Mode="In" Type="Int32" /&gt; &lt;/FunctionImport&gt; &lt;/EntityContainer&gt; &lt;EntityType Name="MyEntity"&gt; &lt;Key&gt; &lt;PropertyRef Name="Id" /&gt; &lt;/Key&gt; &lt;Property Name="Id" Type="Int32" Nullable="false" /&gt; &lt;Property Name="AddressId" Type="Int32" Nullable="true" /&gt; &lt;/EntityType&gt; &lt;/Schema&gt; &lt;/edmx:ConceptualModels&gt; &lt;!-- C-S mapping content --&gt; &lt;edmx:Mappings&gt; &lt;Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS"&gt; &lt;EntityContainerMapping StorageEntityContainer="MyProjectModelStoreContainer" CdmEntityContainer="MyProjectViewEntities" &gt; &lt;FunctionImportMapping FunctionImportName="GetMyEntity" FunctionName="MyProjectModel.Store.sp_getMyEntity" /&gt; &lt;EntitySetMapping Name="MyEntitySet"&gt; &lt;EntityTypeMapping TypeName="IsTypeOf(MyProjectModel.MyEntity)"&gt; &lt;MappingFragment StoreEntitySet="MyEntitySet"&gt; &lt;ScalarProperty Name="AddressId" ColumnName="AddressId" /&gt; &lt;ScalarProperty Name="Id" ColumnName="Id" /&gt; &lt;/MappingFragment&gt; &lt;/EntityTypeMapping&gt; &lt;/EntitySetMapping&gt; &lt;/EntityContainerMapping&gt; &lt;/Mapping&gt; &lt;/edmx:Mappings&gt; &lt;/edmx:Runtime&gt; &lt;!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) --&gt; &lt;edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx"&gt; &lt;edmx:Connection&gt; &lt;DesignerInfoPropertySet&gt; &lt;DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /&gt; &lt;/DesignerInfoPropertySet&gt; &lt;/edmx:Connection&gt; &lt;edmx:Options&gt; &lt;DesignerInfoPropertySet&gt; &lt;DesignerProperty Name="ValidateOnBuild" Value="true" /&gt; &lt;/DesignerInfoPropertySet&gt; &lt;/edmx:Options&gt; &lt;!-- Diagram content (shape and connector positions) --&gt; &lt;edmx:Diagrams&gt; &lt;Diagram Name="MyProjectModel" ZoomLevel="100" &gt; &lt;EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.25" PointY="0.5" Height="7.8375048828125" /&gt; &lt;EntityTypeShape EntityType="MyProjectModel.MyEntity" Width="1.5" PointX="5.5" PointY="1.375" Height="1.2636116536458335" /&gt;&lt;/Diagram&gt;&lt;/edmx:Diagrams&gt; &lt;/edmx:Designer&gt; &lt;/edmx:Edmx&gt; </code></pre> <p>Any ideas why the MyEntity's are not unique?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload