Is there a way to get LinqPad to work with the EF Core context?

I am trying to find out if there is something that I am missing, or some way to crack the lack of support (for now) for the Entity Framework Core DbContexts in LinqPad. I compiled the code targeting 4.6.1 (as suggested on the LinqPad forum ) and tried the "Entity Framework V7" driver, but as its name suggests, I donโ€™t believe it is a date. It still asks for the app.config file or connection string for the constructor.

Add context dialog

Since EF Core contexts use DbContextOptions for construction, not for connection strings, I thought I could create a constructor overload that accepts the connection string but does not process the base database driver. Is there a way to specify a factory to build context? Are there any other options? I feel lost without LinqPad.

+12
source share
2 answers

The latest EFCore 1.1 LINQPad driver (v1.1.1.1) can correctly use a constructor that accepts a string (when this option is selected in LINQPad).

So you can add the following constructor:

 public ApplicationDbContext(string connectionString) : this(new DbContextOptionsBuilder<ApplicationDbContext>() .UseSqlServer(connectionString).Options) { } 

This will tightly associate this context instance with the SQL server provider, but at least not with the connection string. And besides, your application is unlikely to ever try to use this constructor, EF Core never expects / promotes ctor. which takes a string.

For added security, you can wrap this constructor in #if DEBUG ... #endif so that it never goes into production.

+9
source

It appears that the driver is malfunctioning / not updating. I found a way around it by modifying the DbContext.

In theory, this should work, but it is not:

 private string _connectionString; public ApplicationDbContext(string connectionString) : base() { _connectionString = connectionString; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (_connectionString == null) base.OnConfiguring(optionsBuilder); // Normal operation // We have a connection string var dbContextOptionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer(_connectionString); base.OnConfiguring(dbContextOptionsBuilder); } 

The LinqPad EF Core driver continues to search for a constructor without parameters, even if you specify "Through the constructor that takes a string." This is similar to a driver error.

So, I gave him what he wanted, an inconspicuous constructor. I had to hardcode the connection string, as the IoC / appsettings.json config reader is not loaded, and I don't feel like loading it separately in DbContext. But it works and allows me to test the EF Core queries in LinqPad from my model.

This works fine for me:

 private bool _isDebug = false; public ApplicationDbContext() : base() { _isDebug = true; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!_isDebug) base.OnConfiguring(optionsBuilder); // Normal operation // We are in debug mode var dbContextOptionsBuilder = new DbContextOptionsBuilder(); // Hardcoded connection string optionsBuilder.UseSqlServer("data source=XXXX;initial catalog=XXXX;persist security info=True;user id=XXXX;password=XXXX;MultipleActiveResultSets=True;App=EntityFramework"); base.OnConfiguring(dbContextOptionsBuilder); } 

This is, besides the existing public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } , of course.

Edit: Be careful, this seems to override the default behavior, which may not be displayed until you deploy to the server.

+5
source

Source: https://habr.com/ru/post/1014272/


All Articles