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:
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?
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.
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 .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With