0

I am getting below error while trying to access the entities in side datacontext.

System.InvalidOperationException: 'No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.'

My code is in .Net standard library and not all the methods to configure options are available(which most of the answers suggest).

Below is my code. I am trying to test it in console application.

TestCode:

static void Main(string[] args)
    { 
        using (MyEntities context = new MyEntities())
        {
            var stdQuery = (from d in context.My_Employees
                            select new { Company = d.COMPANY_NAME, Emp = d.EMP_NAME });
            foreach (var q in stdQuery)
            {
                Console.WriteLine("Company Name : " + q.Company + ", Emp Name : " + q.Emp);
            }
            Console.ReadKey();
        }
    }

DataContext code is below(.Net standard library)

     public partial class MyEntities : DbContext
    { 
     public virtual DbSet<My_Employee> My_Employees { get; set; }
        public virtual DbSet<My_Company> My_Companies { get; set; }

 protected override void OnConfiguring(DbContextOptionsBuilder builder)
        {
            if (!builder.IsConfigured)
            {
                string conn ="<ConnectionString>"; 
                //builder.  <There is no option to mention UseSqlServer>
            }
            base.OnConfiguring(builder);
        }
}

Any idea how to resolve this? I am really stuck there.

2
  • stackoverflow.com/q/43098065/2864740 - found via refined 'issue' query of "UseSqlServer .net standard". Commented May 30, 2020 at 1:36
  • 1
    Thanks @user2864740. It helped me to resolve the issue. Commented May 30, 2020 at 5:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.