Search code examples
c#entity-framework.net-coreentity-framework-corelinqpad

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?


Solution

  • 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.