Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How Do I query for data using raw sql in a "non entity type format" #22742

Closed
ajcvickers opened this issue Sep 24, 2020 · 0 comments
Closed

How Do I query for data using raw sql in a "non entity type format" #22742

ajcvickers opened this issue Sep 24, 2020 · 0 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ajcvickers
Copy link
Contributor

Moved from discussion #22214

Question

From

I want to pull some data from the db, the SQL to produce it is complicated and the resulting format of that data is not matching a table in the DB.

So how do I tell EF "build this entity set but don't make a table for it".

Example method defined on my DbContext class ...

public IEnumerable<TopCompany> TopCompanies(string system, Perspective perspective, string calendarMonth)
{
    var period = DateTimeOffset.Parse($"{calendarMonth}-01");
    return Set<TopCompany>().FromSqlRaw(" <blob of SQL> ", system, period, period.AddMonths(1));
}

Configured using ...

void ConfigureModel(ModelBuilder builder)
{
    builder.Entity<TopCompany>().HasNoKey();
    ...

I thought this would be enough ... If I make this a DbSet will it not attempt to construct a table in the DB for it?

Replies

From @TehWardy

Never mind ... I just need to read more before asking questions ...
https://stackoverflow.com/questions/35631903/raw-sql-query-without-dbset-entity-framework-core

From @ErikEJ

If you want to avoid "pulluting" your DbContext, you can use this approach:
https://github.com/ErikEJ/EFCorePowerTools/blob/master/src/GUI/RevEng.Core/DbContextExtensions#L10

From @TehWardy

Interesting idea ... but the query in question is specific to a particular context / db.
I just couldn't understand why I had to declare a DbSet<> which I natuallr y think of as a "thing that exists in the db" when this was clearly just a computed set that didn't.

I thought Query,FromSql() like we had in 2.2 was a good idea and was disappointed to see it go.

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. customer-reported labels Sep 24, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

1 participant