6

Using Entity Framework 6, the code below queries a table (really, a view) and returns some number of rows. The number of rows is correct, but many of the rows are null, i.e. some of the Licenses objects are null, as seen in the Visual Studio Watch window. I think what is happening is that if any column of a given row in the view contains a null, then the entire row is being set to null. Any ideas on how to get all the correct data?

String query = "select * from dbo.v_Licenses where [Number] like '%ab%'";
System.Data.Entity.Infrastructure.DbRawSqlQuery<Licenses> dbRawSqlQuery = db.Database.SqlQuery<Licenses>(query);
Queryable<Licenses> licenses = dbRawSqlQuery.AsQueryable();
2
  • 1
    What do you mean a "row" is null? You mean some of the Licenses objects are null? Commented Jun 26, 2014 at 16:18
  • Yes, thanks for pointing out the ambiguity. Clarification added. Commented Jun 26, 2014 at 16:20

3 Answers 3

6

Based on this question it seems like EF (at least at one point) returned null objects if the first column of the result set was null. So rather then selecting * you should explicitly name the columns and ensure that the PK (or some other column) is first.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for this suggestion as well. I changed the code to explicitly specify the columns with the first column being an Id which is defined as (int, not null) thus guaranteeing that the first column cannot have a null value, which I can also verify by looking at the view in SQL Server Management Studio. Nonetheless, I am still getting the same null objects.
Reading the question you linked to ... the last answer (from Serg Rogovtsev) talks about NULLABLE columns. Some of the columns in my view are marked not null and some are marked null. Do I have to specify something in the C# code to distinguish these columns?
Did you build the Licenses using code first or database first? If some of those columns (except strings) are nullable you may need to define them as a nullable type (e.g. int?).
Database first. As it happens, all columns except for the aforementioned Id are varchar. I am playing with [Required] and [Column(Order=x)] at the moment.
The fix for me is to specify [Column(Order=0)] for the Id column. I did this and also changed the select statement back to * instead of specifying the columns. With this I get no null objects.
6

Be sure that License properties'name are identical with columns that are fetched from the select and property type are identical, too.

Like this:

Select CustomerId,Firstname from Customers

public class Customer
{
      public int CustomerId{get;set;}

      public string Firstname {get;set;}
}

and change System.Data.Entity.Infrastructure.DbRawSqlQuery to List

I used this approach a lot and it worked very nice for me.

1 Comment

Thanks for the suggestions. I have verified that the Property names and Column names are identical
5

In my case it did not work because I did NOT have the fields as properties. Once I converted the fields to properties it worked just fine.

1 Comment

This was precisely my problem as well. I was getting the correct number of rows, but all of my fields were null. I switched to properties and all was well.

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.