Search code examples
linqpaddevart

How can I use LinqPad's generated context in Visual Studio


This is a follow-on from this question really:

Moving From LINQpad to a Proper Visual Studio Project?

..but I'm not able to get it to work properly.

An answer to that question suggestions dumping the context assembly out as a dll but although I have done that, when I import it as a reference, it's not exactly clear to me how I would create an instance of that context, point it at a database and actually run a query against it, something like the following:

var db = new ContextFromThatDLL(myconnectionstring);

var query = from a in db.MYTABLE where a.ID == 1 select a;


Extra information:

I am using the IQ driver in LinqPad to connect to Oracle.

I do have a license for DevArt already (which the IQ driver uses) but am aware that the IQ driver generates its own SQL from LINQ - and I prefer it. Plus, I develop queries in LinqPad which works great for my workflow but find that DevArt doesn't always generate SQL as good as IQ.


Solution

  • First, extract the typed data context in LINQPad as follows:

    string dcPath = GetType().BaseType.Assembly.Location;
    string targetFolder = @"c:\temp";
    File.Copy (dcPath, Path.Combine (targetFolder, Path.GetFileName (dcPath)));
    

    Then in Visual Studio, reference the typed data context DLL, along with the following DLLs from the driver folder:

    • IQDriver.dll
    • IQToolkit.dll
    • IQToolkit.Data.dll
    • IQToolkit.Data.(provider).dll

    plus the DevArt driver.

    Then, you can instantiate the typed data context as follows (this illustrates how to do it for SQLite):

    var dc = new LINQPad.User.TypedDataContext (IQToolkit.Data.DbEntityProvider.From
       ("IQToolkit.Data.Sqlite", @"Data Source=D:\SQLite.NET\nutshell.db",
        "LINQPad.User.TypedDataContext"));
    
    var customerCount = dc.Customers.Count();
    

    This should get you started. Bear in mind the caveats, as stated in the answer to which you linked!