Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linqpad

devart

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.

like image 225
Neil Trodden Avatar asked Mar 12 '13 10:03

Neil Trodden


People also ask

How to Add Connection in LINQPad?

Start by launching LINQPad, and then click the Add Connection hyperlink to bring up the Choose Data Context dialog. Select the Use a typed data context from your own assembly option, and then select the Entity Framework (DbContext) option from the list displayed under the option button. Click Next to continue.

Why use LINQPad?

LINQPad lets you query Entity Framework models that you define in Visual Studio. This provides instant feedback, as well as enabling you to see the SQL that your queries generate (just click the SQL tab).

How to Add LINQ to SQL in Visual Studio?

To install the LINQ to SQL tools, start the Visual Studio installer, choose Modify, then select the Individual Components tab, and then select LINQ to SQL tools under the Code Tools category.


1 Answers

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!

like image 182
Joe Albahari Avatar answered Oct 31 '22 18:10

Joe Albahari