does not have a corresponding column in the data reader with the same name

entity-framework

Question

I have Table named tbl_search with columns : id(int), title(nvarchar100), result(ntext) and i want using SQL query, like this :

    using (var db = new GoogleEntities())
      {
          const string selectCmd =
                @"Select top 1 title From tbl_search Where title=@title and id=@id ";

         var data = db.Database.SqlQuery<tbl_search>(
                selectCmd,
                new SqlParameter("@title", "wcf"),
                new SqlParameter("@id", 1)
                ).FirstOrDefault();



           if (data != null)
               {
                var serviceMember = data.ToString();
                label1.Text = serviceMember == "" ? "" : (serviceMember == "True" ? "On" : "Off");
               }
       }

but it give me an error :

The data reader is incompatible with the specified 'GoogleModel.tbl_search'. A member of the type, 'id', does not have a corresponding column in the data reader with the same name.

NOTE: this is my tbl_search class :

 public partial class tbl_search
{
    public int id { get; set; }
    public string title { get; set; }
    public string result { get; set; }
}

i have id in my table.. What is the problem!!

1
17
7/14/2014 8:24:41 AM

Accepted Answer

Your SQL Statement only returns the title not the full entity.

Change:

Select top 1 title From tbl_search Where title=@title and id=@id 

to:

Select top 1 * From tbl_search Where title=@title and id=@id 
13
7/14/2014 9:48:43 AM

Popular Answer

I did the following:

  1. Verified that the stored procedure was up-to-date (the "live" columns were updated).
  2. In EF, I removed, saved, then re-added the stored proc.
  3. Rebuilt and verified all of the types were mapped correctly (no errors).

After doing the above, the error was gone.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow