LinqPad with custom DbContext in custom DLL - CS0120 error


I followed various links I found on the web to add a custom database connection using my own dbContext in my own DLL.
I have a poorly designed MySQL database that I need to connect to (an existing system I have been asked to write some web interfaces to) - I cannot modify the database schema at all.
The database has no physical foreign keys defined at all - each table has a PK and fields that would link to PKs in other tables, but PKs are not physically defined in the database - all logical PKs.
If I connect to MySQL directly - I can see the tables, but tables do not relate to each other,so Linq queries become cumbersome.
I then added a reference to my own dll that contains my dbContext - the UI refreshes and the list of tables and relationships shows correctly.

The problem I have, is that as soon as I try query something - I get the following error:
CS0120 An object reference is required for the non-static field, method, or property 'GradeXpertContext.System'
GradeXpertContext is the name of my class the inherits from DbContext.

Can you please tell me what I need to do in order to be able to query data and start writing Linq queries etc.?



  • Are you using the DbContext driver in LINQPad ("Add connection", "use a typed datacontext from your own assembly"), or are you just pressing F4 and adding a reference to your assembly?

    A trick you can use to diagnose these types of problem is start with a new fresh datacontext with just a couple of tables, and make sure that works first. That might give you a clue as to the cause.
  • Hi Joe,

    I'm using a typed datacontext from my own assembly.
    I've specified the full connection string as well as linked to the config file
    LinqPad is able to use that info to construct the list of tables and entities etc. - as I mentioned previously, the physical database does not have any foreign keys defined.
    If I connect to the database directly, the relationships do not exist - so LinqPad is obviously able to read the context from my assembly.
  • edited February 2019

    I have a similar problem with the error "CS0120 An object reference is required for the non-static field, method, or property FntDbContext.System"

    Maybe the culprit is that a table in my MySql has the name `system` which when scaffolded in EF Core is generated as the entity `System`.

    Can you check if you have a table `system` in your database?

    I have no problems with accessing and running in an ASP.NET Core MVC solution though,
    I've tried for ease of development to use LinqPad, but after successfully creating the Connection with the EF Core driver, I cannot make a single query due to the aforementioned error.
  • Just a follow-up.
    After changing the database table name from `system` to `machinery` everything is fine.
    This, for me, is proof that LinqPad (version 5.36.03) has this odd behaviour and "choke" if the table name is system
  • edited September 2019
    Can confirm. This definitely appears to be a Linqpad bug in v5.40.00. I'm seeing the same issue when I have a table named "System" in my schema and am using my own data context assembly. Funny thing is this was working not too long ago. Both LinqPad and EF Core have been updated since then though.

    There appears to be a workaround though. It's working even without renaming my "System" table if I change the script to be a C# Program, place the query I want in Main(), and add an OnConfiguring() function in the script like so:

    void Main()

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    if (!optionsBuilder.IsConfigured)
    optionsBuilder.UseSqlServer("Data Source=datasource;Initial Catalog=catalog;User Id=user;Password=password");
  • I'm having this issue as well with LinqPad 6, but the workaround of overriding OnConfiguring no longer works.
Sign In or Register to comment.