How to run SQLite EFCore 2.0 in LinqPad?
a simple code I was trying to help from StackOveflow:
nuget packages used:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite
program in C#
----
void Main()
{
var connection = new Microsoft.Data.Sqlite.SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new Microsoft.EntityFrameworkCore.DbContextOptionsBuilder()
.UseSqlite(connection)
.Options;
var context = new TestDbContext(options);
context.Database.EnsureCreated();
var items = new List
{
new Item{Id = 1, Name = "Item 1", Price = 100},
new Item{Id = 2, Name = "Item 2", Price = 10},
new Item{Id = 3, Name = "Item 3", Price = 0},
new Item{Id = 4, Name = "Item 4", Price = -1},
new Item{Id = 5, Name = "Item 5", Price = -5}
};
var query1 = items.Where(x => x.Price > 0).Select(x => x.Name);
var results1 = query1.ToArray();
($"Items with price greater than 0 - {string.Join(",", results1)}").Dump("results 1");
var query2 = items.Where(x => x.Price > -1m).Select(x => x.Name);
var results2 = query2.ToArray();
($"Items with price greater than -1 - {string.Join(",", results2)}").Dump("results 2");
var query3 = items.Where(x => decimal.Negate(x.Price) < 1).Select(x => x.Name);
var results3 = query3.ToArray();
($"Items with price greater than -1 - {string.Join(",", results3)}").Dump("results 3");
}
public class Item
{
public long Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
class TestDbContext : Microsoft.EntityFrameworkCore.DbContext
{
public TestDbContext(Microsoft.EntityFrameworkCore.DbContextOptions options) : base(options) { }
public Microsoft.EntityFrameworkCore.DbSet Items { get; set; }
}
----
I get an error:
> Unable to load DLL 'e_sqlite3': The specified module could not be found.
before I was getting
> Could not load file or assembly 'SQLitePCLRaw.provider.e_sqlite3...
but I could, somehow, move along
Thank you.
nuget packages used:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite
program in C#
----
void Main()
{
var connection = new Microsoft.Data.Sqlite.SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new Microsoft.EntityFrameworkCore.DbContextOptionsBuilder()
.UseSqlite(connection)
.Options;
var context = new TestDbContext(options);
context.Database.EnsureCreated();
var items = new List
{
new Item{Id = 1, Name = "Item 1", Price = 100},
new Item{Id = 2, Name = "Item 2", Price = 10},
new Item{Id = 3, Name = "Item 3", Price = 0},
new Item{Id = 4, Name = "Item 4", Price = -1},
new Item{Id = 5, Name = "Item 5", Price = -5}
};
var query1 = items.Where(x => x.Price > 0).Select(x => x.Name);
var results1 = query1.ToArray();
($"Items with price greater than 0 - {string.Join(",", results1)}").Dump("results 1");
var query2 = items.Where(x => x.Price > -1m).Select(x => x.Name);
var results2 = query2.ToArray();
($"Items with price greater than -1 - {string.Join(",", results2)}").Dump("results 2");
var query3 = items.Where(x => decimal.Negate(x.Price) < 1).Select(x => x.Name);
var results3 = query3.ToArray();
($"Items with price greater than -1 - {string.Join(",", results3)}").Dump("results 3");
}
public class Item
{
public long Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
class TestDbContext : Microsoft.EntityFrameworkCore.DbContext
{
public TestDbContext(Microsoft.EntityFrameworkCore.DbContextOptions options) : base(options) { }
public Microsoft.EntityFrameworkCore.DbSet Items { get; set; }
}
----
I get an error:
> Unable to load DLL 'e_sqlite3': The specified module could not be found.
before I was getting
> Could not load file or assembly 'SQLitePCLRaw.provider.e_sqlite3...
but I could, somehow, move along
Thank you.
Comments
I happen to have a 4.7.1 project which was using the context as well. From the output folder of that project, I copied the following into the same directory as Linqpad. Maybe some of these are extra, but I was just happy to have it working, and not feeling like messing with it further.
SQLitePCLRaw.batteries_green.dll
SQLitePCLRaw.batteries_v2.dll
SQLitePCLRaw.core.dll
SQLitePCLRaw.provider.e_sqlite3.dll
x64\
x86\
To get SQLite working in LINQPad, I've done the following:
=== One time step ===
===
1. In the folder that contains your LINQPad.exe, create the folders ./x64 and ./X86.
2. From the package SQLitePCLRaw.lib.e_sqlite3.v110_xp, extract the e_sqlite3.dll(s) into their corresponding folders;i.e., ../win-x64/.. into x64 and ../win-x86/.. into x86.
3. Your LINQPad folder should look like this:
LINQPAD.exe
/x64/e_sqlite3.dll
/x86/e_sqlite3.dll
4. If you have LINQPad.exe running, you will need to restart the process.
=== When creating an EntityFrameworkCore SQLite snippet ===
===
5. Add NuGet reference to 'SQLitePCLRaw.provider.e_sqlite3.net45'
* Note. I do this for both the LINQPad_AnyCPU version and the LINQPad_x86 version (64bit machines). It's just easier to copy that folder structure to all my machines ;-)
I've not searched the forums (linqpad) to see if there's a way to handle nuget packages that include native assemblies; there may be an opportunity here (to update LINQPAD to handle native dlls from packages).
For some interesting reading about the sqlite packages involved, see https://github.com/ericsink/SQLitePCL.raw .