Is there a LINQPad equivalent to a DataContext class?
Asked Answered
H

6

25

I've just begun using LINQPad and so far I like it but most tutorials I have come across for LINQ TO SQL make use of a DataContext class which is generated by Visual Studio for persisting updates etc. I am also fairly new to LINQ TO SQL so my question is what is the equivalent of the following in LINQPad (if there is one)...

MyDbDataContext db = new MyDbDataContext();

...

db.SubmitChanges();
Hazlett answered 12/5, 2009 at 13:8 Comment(1)
The class that your code is generated into is part of the generated DataContext for the selected database. If you need to refer to the context your LINQPad code, use "this". Note: LINQPad doesn't generate the context exactly the same as LINQ to SQL does, so some edge cases may not work the same in LINQPad as they do with LINQ to Sql.Amara
G
32

Short answer: You do not need to create the DataContext yourself. LINQPad comes with lots of samples, take a look at them.

When you connect LINQPad to a database, it creates the DataContext for you. The DataContext's tables (Table<T>) and SubmitChanges() are available as local members.

For example, LINQPad's default "C# Expression" mode you can just write:

from p in Person
where p.Name == "Joe"
select p.Address

In LINQPad's "C# Statement" mode:

var query = from p in Person
            where p.Name == "Joe"
            select p.Address;

query.Dump(); // Dump() shows results below

Person joe = query.First();
joe.Name = "Peter";
SubmitChanges();

joe.Dump(); // shows joe's values under the previous query results

LINQPad's Dump() extension method is very useful can be called on any object or collection (in LINQPad's statement mode) to show the results below.

Note that you don't even need to connect to a database to use LINQPad. You can work with in-memory collections:

int[] numbers = new[] { 1, 2, 3, 4, 5 };
numbers.Where(n => n > 3).Select(n => n * 2).Dump();

In fact, you don't even need to use LINQ to use LINQPad. It also works great as a snippet compiler.

Globeflower answered 12/5, 2009 at 13:30 Comment(2)
What about inserting new records with LINQ to SQL and LINQPad? I tried inserting by invoking InsertOnSubmit() on a Table<TEntity> in my database with an identity column and I got an exception "The column cannot be modified". The Id was never specified.Scabby
@jlafay probably long solved by now, but perhaps your TEntity didn't have a Primary Key setted.Inglebert
G
20

As it was mentioned before, you don't need to create a DataContext as LINQPad creates one by default.
But just in case, you need a second DataContext (for the same database) you could use

var secondDataContext = new UserQuery();

This will create a second datacontext just like the automatically created one.

Gussiegussman answered 4/5, 2011 at 10:35 Comment(1)
Thanks! That is what is needed to copy and paste queries from visual studio to linqpad with only this small change otherwise have to remove the db context!Halfhour
M
19

I know this already has an answer and I agree with Lucas but wanted to add a couple of things that might help readers of this question.

You can load your own DataContext from the assembly if you want to. Regardless of whether you load your own Context or let LinqPad build one for you, you are running in the Context of a "UserQuery" class that is generated by LinqPad.

The following C# statment shows this:

  this.GetType().Name.Dump();  // Shows UserQuery

This UserQuery class derives from a DataContext. In the this example I let Linqpad build a datacontext for the AdventureWorks database:

  this.GetType().BaseType.Dump();  // Shows TypedDataContext

If I load my own DataContext called MyDataContext:

  this.GetType().BaseType.Dump();  // Shows MyDataContext
Mimetic answered 24/7, 2009 at 16:33 Comment(1)
Helped me figure out what I needed to pass in the constructor of a class to be able to run queries in that scope. public class SubClass { public SubClass(TypedDataContext db) { db.Users.Where(x => x.IsActive); } } and creating it with new SubClass(this);Ashford
R
10

Building on the reply from jaraics, I found that the UserQuery constructor requires a connection string. At least it does for LINQPad version 4.37.11. Therefore, I retrieved the connection string from the UserQuery instance created by LINQPad and used that to create my own instance.

var connectionString = this.Connection.ConnectionString;
var secondDataContext = new UserQuery(connectionString);

When I used the above code, it gave me a second DataContext.

Rumormonger answered 5/3, 2012 at 13:53 Comment(2)
FWIW, this no longer works (at least with 4.42.01) since the ctor now takes a param of IDbConnection instead, so now you can just do var secondDataContext = new UserQuery(this.Connection);Nordrheinwestfalen
Using LINQPad 5, I applied Andrew's advice as follows: using (var dc = new PulseDataContext(this.Connection.ConnectionString)) { /* your update/insert code here */ dc.SaveChanges(); }First
O
3

As per @Thymine 's comment on @DRS9222 's answer, you can simply use something like:

void Main()
{
    TypedDataContext db = this;

    var qry = from i in db.Items
        select i;
    // etc. etc.
}

This becomes pretty transferable between LinqPad and your IDE then, rather than have to fixup or remove the context.

Oreilly answered 2/7, 2020 at 12:9 Comment(0)
N
2

I can access with following sample whenever I add a static method outside the main part:

using(var VT = new LINQPad.User.TypedDataContext())
  return (from g in VT.GM_MEMBERS  where g.Username == username select new { g.Password }).FirstOrDefault()?.Password;
Neral answered 2/3, 2017 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.