简体   繁体   English

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

[英]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. 我想在LinqPad中测试我的EF Core DbContext ,就像我通常使用常规EF一样。

I have a simple DbContext : 我有一个简单的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: 我使用新的EF Core 2.0.1驱动程序在LinqPad中注册了它:

在此输入图像描述 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. 在MCV Core应用程序中,我在应用程序构建器中注册EF和SQL驱动程序,并将其传递给连接字符串。 I don't see how to do the same config in LinqPad? 我不知道如何在LinqPad中进行相同的配置?

In Linqpad there is no dependency injection framework running. 在Linqpad中没有运行依赖注入框架。 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 您可以将连接字符串存储在成员变量-say, _connString - 并在上下文的OnConfiguring覆盖中执行

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

That gives you all freedom to play with database connections without having to leave Linqpad. 这使您可以自由地使用数据库连接而无需离开Linqpad。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LinqPad实体框架和NullExceptionReference - LinqPad Entity Framework and the NullExceptionReference 如何在实体框架和 EF Core 中使用 .Include() - How to use .Include() in Entity Framework and EF Core 如何在 Entity Framework Core 中使用抽象 class? - How to use abstract class in Entity Framework Core? 如何在 Entity Framework Core 中使用 ValueGeneratedOnUpdateSometimes - How to use ValueGeneratedOnUpdateSometimes in Entity Framework Core 在Linqpad中获取实体框架上下文? - Getting entity framework context in Linqpad? 使用内置的 Linq to SQL 驱动程序在 LinqPad 中运行 Entity Framework Core 查询的更简单方法? - Easier way to run Entity Framework Core queries in LinqPad with builtin Linq to SQL driver? 如何在 Blazor 和 Web API 和一个 ZEFE90A8E604A7C840E888D03A 中使用 Entity Framework Core - How to use Entity Framework Core in Blazor and Web API with one package 如何在Entity Framework Core中使用类似于参考表的表 - How to use a table like a reference table in Entity Framework Core 如何在Xamarin形式的SQL Server中使用Entity Framework Core? - How to use Entity Framework Core with SQL Server in Xamarin Forms? 如何在实体框架核心中使用泛型类型? - How can I use a generic type with entity framework core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM