efcore
efcore copied to clipboard
Support structs in DbContext.Database.SqlQuery<T>()
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.
using var ctx = new NorthwindContext();
var price = 40M;
var result = ctx.Database.SqlQuery<NameAndPrice>(
$"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
Using a ValueConverter and ConfigureConventions doesn't work either:
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder
.Properties<MyType>()
.HaveConversion<MyType.EfCoreValueConverter>();
}
I'm using StronglyTypedId