locked
Assigning default value to Complex Entity Framework class when stored procedure doesn't have a column for it. RRS feed

  • Question

  • Hello,


    I have a stored procedure that returns a set of fields. I'm using Entity Framework to connect the results of my stored procedure with Complex object. For the current stored procedure, I would like to use UniqueSafeRecProfile:

        public partial class UniqueSafeRecProfile
        {
            public int ProjectId { get; set; }
            public string Category { get; set; }
            public string Color { get; set; }
            public string HazopCode { get; set; }
            public string ClientColor { get; set; }
            public string Label { get; set; }
            public string ConsequenceCategory { get; set; }
            public Nullable<int> UniqueCount { get; set; }
        }

    This class is used by other stored procedures in the application and it should be perfectly fine for the current one except that the current one doesn't return a value for ConsequenceCategory. Because if this, the application crashes when it tries to return the results from a call to the stored procedure with this error message:

    "The data reader is incompatible with the specified 'RiskAliveModel.UniqueSafeRecProfile'. A member of the type, 'ConsequenceCategory', does not have a corresponding column in the data reader with the same name."

    I can I tell it to assign a default value of null to ConsequenceCategory when it doesn't have a column for it in the results of the stored procedure call?

    Thank you.

    • Moved by Kristin Xie Friday, November 11, 2016 7:13 AM
    Wednesday, November 9, 2016 9:40 PM

All replies

  • Hi gib898,

    Like your title mentioned, your case more related to Entity Framework, I will move your case to EF forum for better support.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 11, 2016 7:13 AM
  • Hi gib898,

    Based on your description, I create a simple demo as below, it works well, could you please provide a bit more information, such as related code, stored procedure, which I could reproduce your issue on our side.

    #Store Procedure.

    CREATE PROCEDURE [dbo].[QueryDept]
    	
    AS
    	SELECT DepartmentId, DepartmentName from Department
    RETURN 0

    #Entity Class

    public  class DepartmentDto
        {
            public int DepartmentId { get; set; }
    
            public string DepartmentName { get; set; }
    
            public string ConsequenceCategory { get; set; }
        }

    #Usage:

    using (var db = new mycon())
                {
                    var result = db.Database.SqlQuery<DepartmentDto>("QueryDept");
    
                    foreach (var item in result)
                    {
                        Console.WriteLine("{0} -- {1} -- {2}", item.DepartmentId, item.DepartmentName, item.ConsequenceCategory);
                    }
                    Console.ReadKey();
                }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 11, 2016 7:21 AM
  • So why can't you stop after the object is built and address the object's ConsequenceCategory property and set it before it is used elsewhere?
    Friday, November 11, 2016 5:57 PM
  • All the code for calling the stored procedure, getting the results, and storing them in a UniqueSafeRecProfile is auto-generated:

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated from a template.
    //
    //     Manual changes to this file may cause unexpected behavior in your application.
    //     Manual changes to this file will be overwritten if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------

        public partial class RiskAliveEntities : DbContext
        {

    ...

        

            public virtual IEnumerable<UniqueSafeRecProfile> GetUniqueSafeguardCount(Nullable<int> projectId)
            {
                var projectIdParameter = projectId.HasValue ?
                    new ObjectParameter("projectId", projectId) :
                    new ObjectParameter("projectId", typeof(int));

                return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<UniqueSafeRecProfile_ForCount>("GetUniqueSafeguardCount", projectIdParameter);
            }

    }

    I'd really prefer not to tamper with it.

    I just thought that since it's auto-generated, there might be a way to tell visual studio to auto-generate some alternative/additional code to handle setting the ConsequenceCategory field to a default value.

    Monday, November 14, 2016 10:40 PM
  • Hi gib898,

    As far as I know, entity framework does not have a method to generate additional code to handle setting the ConsequenceCategory field to a default value, I would suggest that you could use Database.SqlQuery method (which I provide a simple demo In the previous reply) to achieve it.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 17, 2016 10:03 AM