zinon
zinon

Reputation: 4664

Data reader is incompatible with the specified model

I'm using a stored-procedure to update fields on a table. I don't want to update all the fields, thus the fields that are not needed were not includes on the stored procedure.

When I'm trying to call the stored-procedure from the controller using the following code I get: The

data reader is incompatible with the specified'EMSMVCModel.Call_Info'. A member of the type, 'ambulanceDispatch',does not have a corresponding column in the data reader with the same name.

 var insert_query = entities.Database.SqlQuery<Call_Info>("exec [dbo].[update_call_info]  @call_info_id, @call_id, @user_id, @ambulanceNum, @patientId, @patientName, @dateOfBirth, @ethnicity, @gender,
                        new SqlParameter("call_info_id", call_info_id),
                        new SqlParameter("call_id", call_id),
                        new SqlParameter("user_id", u_id),
                        new SqlParameter("ambulanceNum", ambulanceNum),
                        new SqlParameter("patientId", patientId),
                        new SqlParameter("patientName", patientName),
                        new SqlParameter("dateOfBirth", dateOfBirth),
                        new SqlParameter("ethnicity", ethnicity),
                        new SqlParameter("gender", gender)
                        )
                        .Select(x => new
                        {
                            x.call_info_id,
                            x.call_id,etUser,
                            x.Call,*/
                            StatusCode = 1 //Success StatusCode
                        }).ToList();

However, my table has some more fields like ambulanceDispatch. My call_info class is the following:

 public partial class Call_Info
    {
        public int call_info_id { get; set; }
        public int call_id { get; set; }
        public string user_id { get; set; }
        public int ambulanceNum { get; set; }
        public string patientId { get; set; }
        public string patientName { get; set; }
        public System.DateTime dateOfBirth { get; set; }
        public string ethnicity { get; set; }
        public Nullable<int> gender { get; set; }
        public System.DateTime ambulanceDispatch { get; set; }
        public System.DateTime arrivalOnScene { get; set; }

    }

In SQL Server the store-procedure is working as can be seen below.

Note my actual stored-procedure has many more fields.

call_info_id    call_id user_id ambulanceNum    patientId   patientName dateOfBirth ethnicity   gender  address_line    post_code_num   allergies   categories  history crash   historyTextOther    crashTextOther  symptoms    skin    monitoring  section1    section2    section3    section4    section5    section6    section7    section8    section9    section10   section11   section12   section13   section14   section15   section16   section17   section18   section19   section20   section21   section22   section23   section24   section25   section26   section27   section28   section29   section30   section31   section32   section33   section34   section35   section36   section37   section38   section39   section40   section41   section42   section43   section44   section45   section46   section47   section48   section49   section50   section51   section52   AP  breaths oxygen  temp    pulse   left    right   resistance  speech  limb    mouth   chemicals   explosives  gases   flammable   radioactive indications interventions   types   doses   diodes  transport   medicineType    medicineDosage  medicinePlace
2   91390   e67db6bc-3866-446e-a2dd-e5504190ac12    42  1   Andreas Georgiou    2000-04-03 00:00:00.000 Cypriot 3   1, Test Street  1234    0   0   0   0   N/A N/A 0   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   N/A N/A N/A N/A N/A N/A N/A N/A 0   0   0   0   0   0   0   0   0   0   0   0   0   0   N/A N/A changeit

Upvotes: 0

Views: 58

Answers (1)

granadaCoder
granadaCoder

Reputation: 27904

You resultset (the output of the stored procedure) does not contain a column called

ambulanceDispatch

https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.database.sqlquery?view=entity-framework-6.2.0

Creates a raw SQL query that will return elements of the given type. The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. The type does not have to be an entity type. The results of this query are never tracked by the context even if the type of object returned is an entity type. Use the SqlQuery(String, Object[]) method to return entities that are tracked by the context.

Note the "The type can be any type that has properties that match the names of the columns returned from the query, "

The names MUST match.

You need to create a POCO object that matches the (all) columnnames and datatypes of the return columns from the stored procedure.

call_info_id
call_id
user_id
ambulanceNum
patientId
patientName
dateOfBirth
ethnicity
gender
address_line
post_code_num
allergies
categories
history
crash
historyTextOther
crashTextOther
symptoms
skin
monitoring
section1
section2
section3
section4
section5
section6
section7
section8
section9
section10
section11
section12
section13
section14
section15
section16
section17
section18
section19
section20
section21
section22
section23
section24
section25
section26
section27
section28
section29
section30
section31
section32
section33
section34
section35
section36
section37
section38
section39
section40
section41
section42
section43
section44
section45
section46
section47
section48
section49
section50
section51
section52
AP
breaths
oxygen
temp
pulse
left
right
resistance
speech
limb
mouth
chemicals
explosives
gases
flammable
radioactive
indications
interventions
types
doses
diodes
transport
medicineType
medicineDosage
medicinePlace

Upvotes: 2

Related Questions