How to use LinqPad with Entity Framework Core?

.net-core c# entity-framework entity-framework-core linqpad

Question

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?

1
6
2/1/2018 11:25:09 AM

Accepted Answer

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.

3
2/1/2018 12:47:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow