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.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!