如何将 LinqPad 与 Entity Framework 核心一起使用?

How to use LinqPad with Entity Framework Core?

我觉得我错过了一些明显的东西。

我想在 LinqPad 中测试我的 EF Core DbContext,就像我通常使用常规 EF 一样。

我有一个简单的DbContext:

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

我已经使用新的 EF Core 2.0.1 驱动程序在 LinqPad 中注册了它:

但接下来呢?

在 MCV Core 应用程序中,我会在应用程序构建器中注册 EF 和 SQL 驱动程序并向其传递一个连接字符串。我看不到如何在 LinqPad 中进行相同的配置?

Linqpad 中没有依赖注入框架运行。因此,如果您想在那里使用上下文,您可以添加一个接受连接字符串的构造函数。您可以将连接字符串存储在成员变量中——比如 _connString——以及上下文的 OnConfiguring 覆盖中 do

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

这让您可以自由使用数据库连接,而无需离开 Linqpad。