locked
Why This Nonsense?? : System.Data.Entity.Core.Objects.ObjectResult`1[Some Stored Procedure]] List RRS feed

  • Question

  • User2130491911 posted

    Ok this is just nuts... and I am guessing this is some microsoft poor QA bug that will never be fixed but I am wondering if you gurus on this forum have some way of working around this:

    I have a stored procedure that can return more than one row of info

    But for some reason when I call the Stored Procedure I get this on the MVC partial view:

    System.Data.Entity.Core.Objects.ObjectResult`1[CMS.Models.SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result] List

    The wizard wrote auto generated the code below and forced me to add a using statement to add :     using System;
        using System.Data.Entity;
        using System.Data.Entity.Core;
        using System.Data.Entity.Core.Objects;
        using System.Data.Entity.Infrastructure;

    here is the source codes. 

            public virtual ObjectResult<SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result> SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS(Nullable<int> contestantID)
            {
                var contestantIDParameter = contestantID.HasValue ?
                    new ObjectParameter("ContestantID", contestantID) :
                    new ObjectParameter("ContestantID", typeof(int));
    
                return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result>("SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS", contestantIDParameter);
    
    
    
            }

    I am totally lost on this

    Because on the view page it displays this:

    System.Data.Entity.Core.Objects.ObjectResult`1[CMS.Models.SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result] List       

    Id I try this 

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result>("SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS", contestantIDParameter).First();

    <div>Severity Code Description Project File Line Suppression State</div> <div>Error CS0029 Cannot implicitly convert type 'CMS.Models.SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result' to 'System.Data.Entity.Core.Objects.ObjectResult<CMS.Models.SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result>' CMS C:\APP_DEV\CMS\CMS\Models\CMS_DM_DATA_Model.Context.cs 61 Active</div>

    One last thing... this happen when there is data and when there is not data...for some reason this just displays and it sucks.         

    Sunday, September 27, 2020 5:03 AM

All replies

  • User1120430333 posted

    CMS.Models.SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result' to 'System.Data.Entity.Core.Objects.ObjectResult<CMS.Models.SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result>' CMS C:\APP_DEV\CMS\CMS\Models\CMS_DM_DATA_Model.Context.cs 61 Active</div>

    It's not like this has not been talked about before with you about casting.

      public virtual ObjectResult<SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result> SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS(Nullable<int> contestantID)
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result>("SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS", contestantIDParameter);
    

    The Result object the method expects to return is in a different namespace than the 'return' of the executed code is returning for its Result object. The Result class has been defined or originates in two different namespace, and .NET will not allow the casting of an object across two different namespace names even if it's the same class/object in both namespaces, which is everything that is bold highlighted before the class/object name Result that signifies their respective namespaces.

    If Appdev01.cs is  in xyz namespace and you have Appdev01.cs is in zzz namespace, then you can't have code returning zzz.Appdev01.cs object instance to an object instance for xyz.Appdev01.cs even if they are the same class definition for Appdev01.cs in both namespaces.  .NET is not going to allow it.

    How you have the Result object defined for the method it expects on the method's signature and the code you are executing to return Result may be forcing two namespaces to be used although there may be only one Result.cs in the entire program.

    1) The method signature should return the Result object for the code that is dong the 'return' execution of code that is returning its Result object.

    2) You leave the method signature as/is, but you do a explicit cast on the 'return' code for the Result object casting it to the Result object the method signature expects.

    https://www.w3schools.com/cs/cs_type_casting.asp

    Sunday, September 27, 2020 9:08 PM
  • User2130491911 posted

    I'm sorry I am not sure what you mean by casting?

    I did not write that source code... the wizard did. All I did was select the stored procedure from the list in the EDMX wizard.

    So I did not cast or write anything. All I know is that When I added to stored procedure. It added those errors so I just commented all of that stuff the wizard created out... is there is a way to remove these stored procedures I would like to know as I will like to avoid using them as What you wrote and posted is way above my head.

    I have no idea what casting is. Sorry.

    Monday, October 5, 2020 10:35 PM
  • User1120430333 posted

    I have no idea what casting is. Sorry.

    https://www.w3schools.com/cs/cs_type_casting.asp#:~:text=Type%20casting%20is%20when%20you,long%20%2D%3E%20float%20%2D%3E%20double

    <copied>

    C# Type Casting

    Type casting is when you assign a value of one data type to another type.

    Implicit Casting

    Implicit casting is done automatically.

    <end>

    public virtual ObjectResult<SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result> SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS(Nullable<int> contestantID)
            {
                var contestantIDParameter = contestantID.HasValue ?
                    new ObjectParameter("ContestantID", contestantID) :
                    new ObjectParameter("ContestantID", typeof(int));
    
                return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result>("SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS", contestantIDParameter);
    
    
    
            }

    The method expects this object System.Data.Entity.Core.Objects.ObjectResult<CMS.Models.SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result  to be returned from the method to whatever is calling the method. 

    But when  the method executes the stored procedure in the method, the stored procedure  it is returning this object from the stored procedure CMS.Models.SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result'

    Now,  .NET see this, and it is trying to do an implicit cast of the object the stored procedure made trying to auto cast it over to  the object the method is suppose to be returning.

    .NET is throwing an exception stating that it cannot implicitly convert/cast the object the store procedure returned over to the  object the method expects to return.

    You have to do and explicit cast of the object returned by the stored procedure over to the object the method expects to return.

    Tuesday, October 6, 2020 5:22 AM
  • User2130491911 posted

    That is interesting by as I said way above my ability to understand. I will just avoid adding this level of complexity to the application. I thought it would be an easy fix and it's not. I will use a sql view if possible to replace what I  was trying to do with the Stored procedure. 

    Thanks for the details. I will post a link in the source code for a follow on programmer will know how to make it work.

    Many thanks! 

    Tuesday, October 6, 2020 3:31 PM
  • User1120430333 posted

    I thought it would be an easy fix and it's not. I will use a sql view if possible to replace what I  was trying to do with the Stored procedure.

    It is an easy fix.

    public virtual ObjectResult<SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result> SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS(Nullable<int> contestantID)
            {  
    Object obj = new Object(); var contestantIDParameter = contestantID.HasValue ? new ObjectParameter("ContestantID", contestantID) : new ObjectParameter("ContestantID", typeof(int)); obj = ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result>("SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS", contestantIDParameter); return (System.Data.Entity.Core.Objects.ObjectResult<CMS.Models.SPROC_GET_CONTESTANTS_SCORE_SUBTOTALS_Result>)obj; }

    I think it should work.

    The base object that all .NET objects derive from is System.Object. By returning the result of the sproc to System.Object, the namespace for the result object is dropped.

    You can then cast 'obj' to the object the method expects to be returned.

    Tuesday, October 6, 2020 6:36 PM