Index out of range exception in FieldNameLookup.GetOrdinal when running SqlQuery EF











up vote
2
down vote

favorite
1












We are getting eventually an IndexOutOfRange exception when running code like this:



result.Data = dbOptima.Database.ExecuteStoredProcedure(
task,
StoredProcedureValues.PROC_GET_TASKS).ToList();


,where ExecuteStoredProcedure does the following:



public static IEnumerable<TResult> ExecuteStoredProcedure<TResult>(this Database database, IStoredProcedure<TResult> procedure, string procedureName)
{
var parameters = CreateSqlParametersFromProperties(procedure);
var format = CreateSPCommand<TResult>(parameters, procedureName);

return database.SqlQuery<TResult>(format, parameters.Cast<object>).ToArray());
}


We cannot reproduce the problem locally, but using Application Insights the exception is registered quite often. The following is a call stack extract:



System.IndexOutOfRangeException:
at System.Data.ProviderBase.FieldNameLookup.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.SqlClient.SqlDataReader.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.TryGetColumnOrdinalFromReader (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndClrType (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__64 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__63 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute (EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Collections.Generic.List`1..ctor (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Linq.Enumerable.ToList (System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)


We've tried to reproduce the problem locally by:




  • making the stored procedure fail.

  • making the stored procedure return 0 results.

  • causing a mapping problem by renaming some model property.


with no success.



Typically, this error happens due to a mapping error in the datareader when you use ADO.NET, but this is not the case since we use EF 6 and it does not happen always, so we cannot really find where the problem is.










share|improve this question






















  • Running into a similar issue; did you ever find a solution?
    – cvocvo
    Nov 20 at 22:45















up vote
2
down vote

favorite
1












We are getting eventually an IndexOutOfRange exception when running code like this:



result.Data = dbOptima.Database.ExecuteStoredProcedure(
task,
StoredProcedureValues.PROC_GET_TASKS).ToList();


,where ExecuteStoredProcedure does the following:



public static IEnumerable<TResult> ExecuteStoredProcedure<TResult>(this Database database, IStoredProcedure<TResult> procedure, string procedureName)
{
var parameters = CreateSqlParametersFromProperties(procedure);
var format = CreateSPCommand<TResult>(parameters, procedureName);

return database.SqlQuery<TResult>(format, parameters.Cast<object>).ToArray());
}


We cannot reproduce the problem locally, but using Application Insights the exception is registered quite often. The following is a call stack extract:



System.IndexOutOfRangeException:
at System.Data.ProviderBase.FieldNameLookup.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.SqlClient.SqlDataReader.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.TryGetColumnOrdinalFromReader (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndClrType (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__64 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__63 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute (EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Collections.Generic.List`1..ctor (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Linq.Enumerable.ToList (System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)


We've tried to reproduce the problem locally by:




  • making the stored procedure fail.

  • making the stored procedure return 0 results.

  • causing a mapping problem by renaming some model property.


with no success.



Typically, this error happens due to a mapping error in the datareader when you use ADO.NET, but this is not the case since we use EF 6 and it does not happen always, so we cannot really find where the problem is.










share|improve this question






















  • Running into a similar issue; did you ever find a solution?
    – cvocvo
    Nov 20 at 22:45













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





We are getting eventually an IndexOutOfRange exception when running code like this:



result.Data = dbOptima.Database.ExecuteStoredProcedure(
task,
StoredProcedureValues.PROC_GET_TASKS).ToList();


,where ExecuteStoredProcedure does the following:



public static IEnumerable<TResult> ExecuteStoredProcedure<TResult>(this Database database, IStoredProcedure<TResult> procedure, string procedureName)
{
var parameters = CreateSqlParametersFromProperties(procedure);
var format = CreateSPCommand<TResult>(parameters, procedureName);

return database.SqlQuery<TResult>(format, parameters.Cast<object>).ToArray());
}


We cannot reproduce the problem locally, but using Application Insights the exception is registered quite often. The following is a call stack extract:



System.IndexOutOfRangeException:
at System.Data.ProviderBase.FieldNameLookup.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.SqlClient.SqlDataReader.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.TryGetColumnOrdinalFromReader (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndClrType (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__64 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__63 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute (EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Collections.Generic.List`1..ctor (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Linq.Enumerable.ToList (System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)


We've tried to reproduce the problem locally by:




  • making the stored procedure fail.

  • making the stored procedure return 0 results.

  • causing a mapping problem by renaming some model property.


with no success.



Typically, this error happens due to a mapping error in the datareader when you use ADO.NET, but this is not the case since we use EF 6 and it does not happen always, so we cannot really find where the problem is.










share|improve this question













We are getting eventually an IndexOutOfRange exception when running code like this:



result.Data = dbOptima.Database.ExecuteStoredProcedure(
task,
StoredProcedureValues.PROC_GET_TASKS).ToList();


,where ExecuteStoredProcedure does the following:



public static IEnumerable<TResult> ExecuteStoredProcedure<TResult>(this Database database, IStoredProcedure<TResult> procedure, string procedureName)
{
var parameters = CreateSqlParametersFromProperties(procedure);
var format = CreateSPCommand<TResult>(parameters, procedureName);

return database.SqlQuery<TResult>(format, parameters.Cast<object>).ToArray());
}


We cannot reproduce the problem locally, but using Application Insights the exception is registered quite often. The following is a call stack extract:



System.IndexOutOfRangeException:
at System.Data.ProviderBase.FieldNameLookup.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.SqlClient.SqlDataReader.GetOrdinal (System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.TryGetColumnOrdinalFromReader (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndClrType (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__64 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext+<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__63 (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute (EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext (EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Collections.Generic.List`1..ctor (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at System.Linq.Enumerable.ToList (System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)


We've tried to reproduce the problem locally by:




  • making the stored procedure fail.

  • making the stored procedure return 0 results.

  • causing a mapping problem by renaming some model property.


with no success.



Typically, this error happens due to a mapping error in the datareader when you use ADO.NET, but this is not the case since we use EF 6 and it does not happen always, so we cannot really find where the problem is.







c# .net asp.net-mvc entity-framework azure-application-insights






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 2 at 20:18









Luis

1,0391327




1,0391327












  • Running into a similar issue; did you ever find a solution?
    – cvocvo
    Nov 20 at 22:45


















  • Running into a similar issue; did you ever find a solution?
    – cvocvo
    Nov 20 at 22:45
















Running into a similar issue; did you ever find a solution?
– cvocvo
Nov 20 at 22:45




Running into a similar issue; did you ever find a solution?
– cvocvo
Nov 20 at 22:45












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










The problem is related to binding the result of the stored procedure with the model declared to be bound with the output of the stored procedure.



If the stored procedure retrieves more columns than the properties that the model has, even though the model is satisfied (because all the properties can be matched with columns of the stored procedure output), internally an exception is raised cause the model doesn't have properties to store some of the store procedure output values.



So, this can be fixed in two ways: either




  • adding properties in the model to contain the columns of the stored
    procedure, or

  • removing the unnecessary columns of the stored procedure output.


The second approach is normally better, since this issue is making evident that some data returned by the stored procedure is no longer required, so it would be cleaner and more performant to remove the unneeded code in the stored procedure.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51143207%2findex-out-of-range-exception-in-fieldnamelookup-getordinal-when-running-sqlquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    The problem is related to binding the result of the stored procedure with the model declared to be bound with the output of the stored procedure.



    If the stored procedure retrieves more columns than the properties that the model has, even though the model is satisfied (because all the properties can be matched with columns of the stored procedure output), internally an exception is raised cause the model doesn't have properties to store some of the store procedure output values.



    So, this can be fixed in two ways: either




    • adding properties in the model to contain the columns of the stored
      procedure, or

    • removing the unnecessary columns of the stored procedure output.


    The second approach is normally better, since this issue is making evident that some data returned by the stored procedure is no longer required, so it would be cleaner and more performant to remove the unneeded code in the stored procedure.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      The problem is related to binding the result of the stored procedure with the model declared to be bound with the output of the stored procedure.



      If the stored procedure retrieves more columns than the properties that the model has, even though the model is satisfied (because all the properties can be matched with columns of the stored procedure output), internally an exception is raised cause the model doesn't have properties to store some of the store procedure output values.



      So, this can be fixed in two ways: either




      • adding properties in the model to contain the columns of the stored
        procedure, or

      • removing the unnecessary columns of the stored procedure output.


      The second approach is normally better, since this issue is making evident that some data returned by the stored procedure is no longer required, so it would be cleaner and more performant to remove the unneeded code in the stored procedure.






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        The problem is related to binding the result of the stored procedure with the model declared to be bound with the output of the stored procedure.



        If the stored procedure retrieves more columns than the properties that the model has, even though the model is satisfied (because all the properties can be matched with columns of the stored procedure output), internally an exception is raised cause the model doesn't have properties to store some of the store procedure output values.



        So, this can be fixed in two ways: either




        • adding properties in the model to contain the columns of the stored
          procedure, or

        • removing the unnecessary columns of the stored procedure output.


        The second approach is normally better, since this issue is making evident that some data returned by the stored procedure is no longer required, so it would be cleaner and more performant to remove the unneeded code in the stored procedure.






        share|improve this answer












        The problem is related to binding the result of the stored procedure with the model declared to be bound with the output of the stored procedure.



        If the stored procedure retrieves more columns than the properties that the model has, even though the model is satisfied (because all the properties can be matched with columns of the stored procedure output), internally an exception is raised cause the model doesn't have properties to store some of the store procedure output values.



        So, this can be fixed in two ways: either




        • adding properties in the model to contain the columns of the stored
          procedure, or

        • removing the unnecessary columns of the stored procedure output.


        The second approach is normally better, since this issue is making evident that some data returned by the stored procedure is no longer required, so it would be cleaner and more performant to remove the unneeded code in the stored procedure.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 19:55









        Luis

        1,0391327




        1,0391327






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51143207%2findex-out-of-range-exception-in-fieldnamelookup-getordinal-when-running-sqlquery%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Catalogne

            Violoncelliste

            Héron pourpré