The data reader is incompatible with the specified Entity Framework

前端 未结 3 1360
旧时难觅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<IFramingSystem>();
    

    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<EmailList>("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<myPOCO> GetMyPoco()
    

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

    public myPOCO GetMyPoco()
    
    0 讨论(0)
  • 2020-12-18 20:27

    If you are inserting/deleting/updating (these are considered by EF as 'non-query'), and can be called by our code using

    MyDbContext.Database.ExecuteSqlCommand(insert into Table (Col1,Col2) values (1,2));
    

    But if are doing select query for a raw SQL statement, then use

    MyDbContext.DbSet<Table_name>.SqlQuery(select * from table_name).ToList();
    

    or

    MyDbContext.Database.SqlQuery(select * from table_name).ToList();
    

    ()

    The SqlQuery() function, in EF, for strange reasons, throw exception Insert/delete/update operation. (The exception thrown is "A member of the type, does not have a corresponding column in the data reader with the same name.") But it has actually performed operation if you open your Sql Management Studio and check for the entries.

    FYI http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx

    0 讨论(0)
  • 2020-12-18 20:38

    To me it seems that both branches of the IF return different data, the first branch returns 9 columns where the second - only three. I believe the EF can't reflect the IFramingSystem from the latter. Specifically, the column FrameType (and 5 other columns) are obviously missing:

     ...
     SELECT c.ID,c.Name,c.Descr    <- where are the remaining columns
        from Catelog.Component c
     ...
    
    0 讨论(0)
提交回复
热议问题