How can I model the result of 'FromSql()' queries to base datatypes?

Viewed 396

I'm using the key-less entity type feature in EF Core 3.1. and I want to map some table data into my entity classes. here is my db-context config:

    public class HimartWarehouseReportContext: DbContext
{
    public HimartWarehouseReportContext()
    {
    }

    public HimartWarehouseReportContext(DbContextOptions<HimartWarehouseReportContext> options)
        : base(options)
    {
    }

    public DbSet<MarketPresence> MarketPresence { get; set; }
    public DbSet<Campaign> Campaign { get; set; }
    public DbSet<CampaignDt> CampaignDt { get; set; }
    public DbSet<List<String>> Category { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<MarketPresence>().HasNoKey().ToView(null);
        modelBuilder.Entity<Campaign>().HasNoKey().ToView(null);
        modelBuilder.Entity<List<String>>().HasNoKey().ToView(null);
    }
}

About my MarketPresence and Campaign classes, everything works fine when I use them like this:

public IEnumerable<Campaign> GetCampaignReports(Campaign input)
{
   var result = _reportContext
                .Campaign
                .FromSqlRaw("GetCampaignReport " + 
                            $"{input.ProvinceId}, " +
                            $"{input.CityId}")
}

but about the last case Category, I just want to return the list of strings as the result of my sql query and I get exception with this message: The required column 'Capacity' was not present in the results of a 'FromSql' operation. (capacity is the name of my sql result query, the result of query is a table with single column named capacity) How can I come up with this issue?

Thanks for your attention.

1 Answers

I had something similar and just made a class like your Category class with several hundred attributes. It was the easiest approach I could come up with.

Related