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

Query Support in entity framework core 3.1 #23923

Closed
KamranShahid opened this issue Jan 20, 2021 · 1 comment
Closed

Query Support in entity framework core 3.1 #23923

KamranShahid opened this issue Jan 20, 2021 · 1 comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@KamranShahid
Copy link

I am using .net core 3.1 application in which i have referenced entity framework core 3.1.8 and using various tables via model first approach. (db is mysql)
Now I need some custom join query where i have to run manual queries.
I am trying code from #1862

public static class SqlQueryExtensions
    {
        public static IList<T> SqlQuery<T>(this DbContext db, string sql, params object[] parameters) where T : class
        {
            using (var db2 = new ContextForQueryType<T>(db.Database.GetDbConnection()))
            {
                return db2.Set<T>().FromSqlRaw(sql, parameters).ToList();
            }
        }

        private class ContextForQueryType<T> : DbContext where T : class
        {
            private readonly DbConnection connection;

            public ContextForQueryType(DbConnection connection)
            {
                this.connection = connection;
            }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(connection, options => options.EnableRetryOnFailure());

                base.OnConfiguring(optionsBuilder);
            }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<T>().HasNoKey();
                base.OnModelCreating(modelBuilder);
            }
        }
    }

but it is not compiling.
Please suggest is it related to nuget package difference or what?

@ErikEJ
Copy link
Contributor

ErikEJ commented Jan 20, 2021

You probably need to change this line to use the MySQL provider (I am in the blind here, since you are not sharing any information about the build errors):

 optionsBuilder.UseSqlServer(connection, options => options.EnableRetryOnFailure());

A production hardened version of this class is available here: https://github.com/ErikEJ/EFCorePowerTools/blob/master/src/GUI/RevEng.Core/DbContextExtensions

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Jan 29, 2021
@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

3 participants