How to use LinqPad with Entity Framework Core?
Asked Answered
T

1

8

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?

Tomkin answered 1/2, 2018 at 11:25 Comment(1)
Thanks @GertArnold, that did the trick. If you want to post that as an answer I will upvote and mark it as accepted.Tomkin
G
5

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.

Geminian answered 1/2, 2018 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.