Home
Options

Have never been able to get LINQ working with EF Core Context in EF6

Absolutely loved linqpad 5 with .NET Framework. But as we have started doing things in Core 3.1 this year, we have not been able to get it working. If I create a connection with a parameterless constructor (which my context does have) and reference a connectionstrings.json file, the Test button fails right in the setup process. If I create a connection via a constructor which contains DBContextOptions<> (which my context also has), the Test button succeeds and I can run SQL queries but I cannot run even the most basic linq queries using the context.

The error I'm getting in both the failure situations is the "No database provider has been configured for this DbContext. ..." error. I reviewed this document:
https://linqpad.blob.core.windows.net/public/SampleDbContext.cs

and I'm just not clear on what I'm supposed to add. I really don't want to have to add a constructor with a connection string solely for the purpose of using Linqpad. I never had to do that before Core and it sounds like these other options are supposed to work. Any advice I can get on what to try would be appreciated. Below is the key code in my context:

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

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
    }

Final note - in the various applications which use the context, it gets injected:
services.AddDbContext(options => { options.UseSqlServer(Configuration.GetConnectionString("AppDBContext")); });

Comments

  • Options

    I eventually figured out the problem. I was writing the LINQ the way I used to with the lonqpad 5 and .net framework, with a using statement instantiating the appcontext. This was causing it to use the parameterless constructor and I didn't have anything in there to apply a connectionstring. I figured out that I don't have to explicitly reference the appcontext in my linq code and then it works with the DBContextOptions and picks up the appsettings

  • Options

    That makes sense. Thanks for reporting back.

Sign In or Register to comment.