Using Entity Framework 6, I was able to use execute a Raw SQL Query and use a custom model which was not defined in the DBContext in order to store the output of the query. A si
follow these steps:
Probably it could be better if you can reduce it to a model as generic as possible but it's not a must:
public class MyCustomModel
{
public string Text { get; set; }
public int Count { get; set; }
}
Create DbSet
for your custom model
public virtual DbSet MyCustomModelName { get; set; }
Keep in mind to specify your custom model has no key
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
...
modelBuilder.Entity().HasNoKey();
}
async public Task> GetMyCustomData()
{
var rv = new List();
using (var dataContext = new DbContext())
{
var sql = @"
select textField as 'Text', count(1) as 'Count'
from MyTable";
rv = await dataContext.Set().FromSqlRaw(sql).ToListAsync();
}
return rv;
}