dotnet / dotnet/efcore

Support structs in DbContext.Database.SqlQuery<T>()

Open
#35,877 1 comment 1 reaction 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### What problem are you trying to solve?

I would like to have value type support. Try this code sample below.

Currently you get this Unhandled exception. `System.ArgumentException: The specified type 'NameAndPrice' must be a non-interface reference type to be used as an entity type`.

```cs
using var ctx = new NorthwindContext();

var price = 40M;

var result = ctx.Database.SqlQuery(
$"select ProductName, UnitPrice from dbo.Products where UnitPrice > {price}");

foreach (var item in result) {
Console.WriteLine($"{item.ProductName} {item.UnitPrice:C2} ");
}

internal record struct NameAndPrice(string ProductName, decimal UnitPrice) {

public static implicit operator (string ProductName, decimal UnitPrice)(NameAndPrice value) {
return (value.ProductName, value.UnitPrice);
}

public static implicit operator NameAndPrice((string ProductName, decimal UnitPrice) value) {
return new NameAndPrice(value.ProductName, value.UnitPrice);
}
}
```

Please remove the non-interface reference type limitation. Dapper supports this, EF Core should do this too.

### Describe the solution you'd like

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.