Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use LinqPad with Entity Framework Core?

I feel like I've missed something obvious.

I want to test my EF Core DbContext in LinqPad like I usually do with regular EF.

I have a simple DbContext:

public class NotificationManagerContext : DbContext
{
    public virtual DbSet<Notification> Notifications { get; set; }        
}

I've registered it in LinqPad using the new EF Core 2.0.1 driver:

enter image description here But what next?

In an MCV Core app I'd register EF and the SQL driver in the app builder and pass it a connection string. I don't see how to do the same config in LinqPad?

like image 989
Kevin Kuszyk Avatar asked Feb 01 '18 11:02

Kevin Kuszyk


People also ask

How to use LINQPad with Entity Framework?

LINQPad can connect to an Entity Data Model by adding a connection, just like LINQ to SQL connections. To do so, click the Add Connection hyperlink to bring up the Choose Data Context dialog. Then, select the option Use a typed data context from your own assembly, along with the Entity Framework (DbContext) option.

What is the use of LINQPad in C#?

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). You can query Entity Framework models in both LINQPad 7 (for .

How can I convert SQL query to LINQ using LINQPad?

Click on "Add connection"; a window will appear. Choose "Default (LINQ to SQL)" and click on the "Next" button. A new window will appear, fill in the required details to get connected with the desired database.

How does LINQPad work?

For each query, LINQPad creates a separate server, which a class that runs in its own process and executes the query in isolation. This isolation prevents queries from interfering with each other (or the UI) and allows LINQPad to safely cancel a query without polluting other application domains.


1 Answers

In Linqpad there is no dependency injection framework running. So if you want to use a context there you could add a constructor that accepts a connection string. You can store the connection string in a member variable —say, _connString— and in the OnConfiguring override of the context do

if (!string.IsNullOrWhiteSpace(_connString)) 
{
    optionsBuilder.UseSqlServer(_connString);
}

That gives you all freedom to play with database connections without having to leave Linqpad.

like image 133
Gert Arnold Avatar answered Sep 29 '22 16:09

Gert Arnold