Is there a LINQPad equivalent to a DataContext class?

孤者浪人 提交于 2019-11-27 13:07:02

问题


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();

回答1:


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.




回答2:


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



回答3:


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.




回答4:


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.




回答5:


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;


来源:https://stackoverflow.com/questions/852687/is-there-a-linqpad-equivalent-to-a-datacontext-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!