3

I'm trying to use the following SQL query to get the first two columns of data out of my database:

SELECT  Id, DomainUrl
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY Id ) AS RowNum, Id, DomainUrl
          FROM      SiteDatas
        ) AS RowConstrainedResult
WHERE   RowNum >= 1
    AND RowNum <= 10
ORDER BY RowNum

I'm using entity framework and passing in parameters for the rows so that it can later be used as a scroll on load function via ajax. However I am getting an error when I get to the line:

var stores = db.SiteDatas.SqlQuery(SQL, parameters).ToList();

It's an entity command execution exception that is stating that the data reader is incompatible with the specified model (my db) member of the type, 'Robots' (which is the next column after the one I am calling), does not have a corresponding column in the data reader with the same name.

How would I go about getting just the first two columns out in a way that can be serialized as json?

5
  • 1
    Does SiteDatas contain a Robots column? Because the query doesn't Commented Jul 16, 2013 at 10:03
  • It does indeed, but I don't want it as a result Commented Jul 16, 2013 at 10:12
  • You can add NULL AS Robots in your select query to return it always as null? Commented Jul 16, 2013 at 10:15
  • There are quite a few other columns, is NULLing them all really the best way to go about it? Commented Jul 16, 2013 at 10:16
  • 1
    Maybe you can create a new class based on this query that have only the needed columns? Commented Jul 16, 2013 at 10:20

1 Answer 1

7

The DbSet.SqlQuery and Database.SqlQuery commands both return entity instances. Your sql query has to return the same columns as your entity. Most probably, your SiteData class contains a Robots column that doesn't exist in your SQL query.

You can still use Database.SqlQuery< T > to return your data, provided you specify the return type. The return type doesn't have to be an entity type, just have the same column names as your result set.

Assuming db is a DbContext instance, you can write:

public class MyResults
{
   public int ID{get;set;}
   public string DomainUrl {get;set;}
}

...

var stores = db.Database.SqlQuery<MyResults>(SQL, parameters).ToList();
Sign up to request clarification or add additional context in comments.

5 Comments

It does, but I don't want it as a result, is there a way around this?
Yes, Database.SqlQuery can return any type whose properties match the result columns
I've already tried this and get the error: The non-generic method 'System.Data.Entity.DbSet<Bescoured.DomainModels.SiteData>.SqlQuery(strin, params object[]) cannot be used with type arguments.
Wrong method! Try Database.SqlQuery<YourCustomClass>, not DbSet.SqlQuery
another important thing is it was not working if we dont put {get;set;} in model, faced this problem thought to share it so that other won't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.