The data reader is incompatible with the specified Entity Framework

前端 未结 3 1362
旧时难觅i
旧时难觅i 2020-12-18 20:10

I have a method that will return the bare min results from a sproc to fill a select menu. When I want the bare min results I pass bool getMin = true to the sproc, and when I

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-18 20:27

    I understand this is an old post; but, I wanted to share what I learned tonight about this. What I found that the 'most relevant portion of the error message is stating' is this.

    db.proc_GetFramingSystems(brandID, frameType, glazeMethod, getMin).ToList();
    

    is expecting a column to be returned from the stored procedure with the alias of 'FrameType'.

    I ran into this when I created a POCO (plain old clr object) class of my table with more programmer friendly names. Instead of a strongly typed name of say 'email_address', I wanted 'EmailAddy' and so forth. I created a mapped class stating this among other mapped columns.

    this.Property(t => t.EmailAddy).HasColumnName("email_address");
    

    Although this is necessary for other parts of EF to work, the mapping class is not referenced when executing a db.SqlQuery. So, when the code below executes

    var a = new SqlParameter("@fshipno", shipno);
    return _context.db.SqlQuery("exec spGetEmailAddy @fshipno", a).ToList();
    

    It generated the same error except instead of 'FrameType', it mentioned 'EmailAddy'. The fix... I had to alias the 'email_address' column in my stored procedure to 'EmailAddy' to get it to map the returned dataset to my POCO.

    EDIT: I have found that this only works if your method is returning an IEnumberable

    public IEnumberable GetMyPoco()
    

    You will get the same error message if you are attempting to return a single POCO object.

    public myPOCO GetMyPoco()
    

提交回复
热议问题