Cannot map raw SQL query to DataRow

I am trying to get IEnumerable from linq query below. What am I doing wrong?

IEnumerable<DataRow> results = 
    context.Database.SqlQuery<DataRow>("SELECT * FROM Customer").AsEnumerable();


DataRow class does not have default (parameterless) constructor, so you can't use it as query parameter type. There is no generic constraints on type parameter, and nothing mentioned on MSDN(!), but column map factory will throw exception if parameter type does not have default constructor:

The result type 'System.Data.DataRow' may not be abstract and must include a default constructor.

Here is a code which throws this exception:

internal static CollectionColumnMap CreateColumnMapFromReaderAndClrType(
    DbDataReader reader, Type type, MetadataWorkspace workspace)
      BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
      ConstructorInfo constructor = type.GetConstructor(flags, (Binder) null, Type.EmptyTypes, (ParameterModifier[]) null);
      if (type.IsAbstract || (ConstructorInfo) null == constructor && !type.IsValueType)
          throw EntityUtil.InvalidOperation(InvalidTypeForStoreQuery((object) type));
      // ...

BTW Mapping to DataRow makes no sense, even if it would have default public constructor. Because it is not simple primitive type and it does not have properties which match the names of columns returned from the query (yes, mapping uses properties only).

Correct usage of Linq will be

IEnumerable<Customer> results = context.Customers;

That will generate SELECT * FROM Customer query, and map query results to customer entities. If you really want to use raw SQL:

IEnumerable<Customer> results = 
     context.Database.SqlQuery<Customer>("SELECT * FROM Customers");

I think we were trying to solve the same problem (Google led me here, anyway). I am executing a raw SQL command through SqlQuery<TElement>(string sql, params object[] parameters and wanted to assert individual properties of the results returned from the query in a unit test.

I called the method:

var result = (db.SqlQuery<Customer>("select * from customers").First();

and verified the data it returned:

Assert.AreEqual("John", result.FirstName);

I defined a private class Customer inside my test class (unfortunately, I'm not using Entity Framework):

private class Customer
    public string FirstName { get; set; }

The properties of Customer must match the column names returned in the SQL query, and they must be properties (not just variables of the Customer class. You don't have to create properties for all of the columns returned from the query.

Need Your Help

Apache 2 Segmentation fault when zend php 53 installed with curl

php apache zend-framework segmentation-fault

I spent much time to figure out why apache2 crashes. My environment is: