I am in the process of replacing Dapper with EF Core in a project. Specifically using the new Database.SqlQuery and Database.ExecuteSql functionality with EF Core. I like how they use FormattableString to handle SQL parameters.
However I am confused on what to do when I would like to use both string interpolation and the curly braces for parameters with the FormattableStrings? Se example below of a Dapper version and a non-working EF Core version. I would not like the "subSql" to be passed as a parameter but just to create the string. How would I approach this?
public IEnumerable<Something> GetSomethingWithDapper(string id, bool enabled, string type)
{
var subSql = switch (type) {
case "A":
"TableA";
break;
case "B":
"TableB";
break;
};
var sql = $"SELECT * FROM {subSql} WHERE Id = @id";
// My dapper request here
}
public IEnumerable<Something> GetSomethingWithEFCore(string id, string type)
{
var subSql = switch (type) {
case "A":
"TableA";
break;
case "B":
"TableB";
break;
};
FormattableString sql = $"SELECT * FROM {subSql} WHERE Id = {id}";
// My EF Core request here
}
var customer=context.Customers.Find(id);
. And if you want the customer's orders,var customer=context.Customers.Include(c=>c.Orders).Find(id);
Support
andIs Better
aren't the same. You're obviously mapping the full table, so why isn'tSomething
part of the model?