Reputation: 95
I am getting an error message that says:
"No mapping exists from object type <>f__AnonymousType6`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type."
public List<Product> GetCategoryProducts(int catID)
{
string sql = @"select p.ProductID, p.ItemName, p.ImageName
from Product p, ProductCategory pc
where p.ProductID = pc.ProductID
and pc.CategoryID = @catID
order by p.ItemName";
List<Product> products = db.Products.SqlQuery(sql, new { catID }).ToList();
return products;
}
I am not sure why I am getting this error
Upvotes: 0
Views: 131
Reputation: 53958
You should do two things a) change the return type of your query, SqlQuery<Product>
and b) pass the parameter catID
to your query:
var products = db.Products
.SqlQuery<Product>(sql,new SqlParameter("catID", catID))
.ToList();
I am setting pc.CategoryID = @catID.
Indeed you declare a parameterized query, but you don't pass a value for your parameter. The following is just a string:
@"select p.ProductID, p.ItemName, p.ImageName
from Product p, ProductCategory pc
where p.ProductID = pc.ProductID
and pc.CategoryID = @catID
order by p.ItemName"
You don't set there any value for the CategoryID
. This is the job of SqlQuery
method, that would handle this provided that you pass one.
Upvotes: 2