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

Generic FromSql #3187

Closed
freshe opened this issue Sep 22, 2015 · 5 comments
Closed

Generic FromSql #3187

freshe opened this issue Sep 22, 2015 · 5 comments

Comments

@freshe
Copy link

freshe commented Sep 22, 2015

FromSql only seems to work with classes/tables that is already mapped within your DbContext.
Is there a way/plan to make it work like the "EF6x SqlQuery generic" way?

The entity type 'Test' was not found. Ensure that the entity type has been added to the model

    public class Test
    {
        public int id { get; set; }
    }

    public void Index()
    {
        var db = new AppDbContext();
        var data = db.Set<Test>().FromSql("SELECT id FROM table WHERE 1=1").ToList();
    }
@anpete
Copy link
Contributor

anpete commented Sep 22, 2015

See #1862

@divega
Copy link
Contributor

divega commented Sep 22, 2015

@freshe this is a known limitation of FromSql() compare to EF6's SqlQuery() as explained in the bug @anpete linked to. Although FromSql() allows you to provide alternative SQL to bootstrap a DbSet, the DbSet can still only be of mapped entity type. All that said, you should be able to do this to get what you want:

var data = db.Set<Table>()
    .FromSql("SELECT * FROM table WHERE 1=1")
    .Select(t => new Test {Id = t.Id})
    .ToList();

Also note that until very recently there was a bug (#3142) that that would cause the projection to be evaluated in memory, but this is now fixed in nightly builds.

@freshe
Copy link
Author

freshe commented Sep 22, 2015

Thanks @divega @anpete for clarification and workarounds.

I have a UnitOfWork class with the SqlQuery method used extensively throughout the application. If FromSql could be used in the same way it would have been easy to update the code in one place and just have it work.

@Vasim-DigitalNexus
Copy link

Same here we use the sqlQuery method extensively throughout applications that are not rooted in the DbContext; typically to retrieve (subset) view data that is not part of the DbContext, therefore item #1862 would be invaluable

@divega
Copy link
Contributor

divega commented Sep 23, 2015

@freshe @Vasimovic thanks for the feedback. I will close this issue as a dupe of #1862 and have that one marked as up-for-grabs. We agree this capability is important but at the moment we don't plan to include it in EF 7.0 RTM.

@divega divega closed this as completed Sep 23, 2015
@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
None yet
Projects
None yet
Development

No branches or pull requests

5 participants