|
|
|
|
@ -0,0 +1,76 @@ |
|
|
|
|
using System.Diagnostics; |
|
|
|
|
using Microsoft.Data.Sqlite; |
|
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure; |
|
|
|
|
|
|
|
|
|
namespace realm_cli; |
|
|
|
|
|
|
|
|
|
public class AppDbContext: DbContext |
|
|
|
|
{ |
|
|
|
|
public DbSet<LiveDataState> LvStates { get; set; } |
|
|
|
|
|
|
|
|
|
public AppDbContext() |
|
|
|
|
{ |
|
|
|
|
SQLitePCL.Batteries_V2.Init(); |
|
|
|
|
this.Database.EnsureCreated(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
|
|
|
|
{ |
|
|
|
|
var db_path = "c:/prjs/ttt.db3"; |
|
|
|
|
var conn = new SqliteConnection($"Filename={db_path}"); |
|
|
|
|
conn.Open(); |
|
|
|
|
IncreasePerformance(conn); |
|
|
|
|
//conn.Close(); |
|
|
|
|
optionsBuilder.UseSqlite(conn); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder) |
|
|
|
|
{ |
|
|
|
|
modelBuilder.Entity<LiveDataState>().ToTable("lv_states").HasKey(i=>i.ItemId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void ExecutePragma(SqliteConnection conn, string pragmaCommand) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
using var command = conn.CreateCommand(); |
|
|
|
|
command.CommandText = pragmaCommand; |
|
|
|
|
command.ExecuteNonQuery(); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
Console.WriteLine($"Unable to execute pragma command {pragmaCommand}: {ex.Message}"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Enable SQLite performance improvements |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="db"></param> |
|
|
|
|
private void IncreasePerformance(SqliteConnection conn) |
|
|
|
|
{ |
|
|
|
|
// Increase the timeout from the default (which I think is 30s) |
|
|
|
|
// To help concurrency. |
|
|
|
|
//Database.SetCommandTimeout(60); |
|
|
|
|
// Enable journal mode - this will also improve |
|
|
|
|
// concurrent acces |
|
|
|
|
//ExecutePragma("PRAGMA journal_mode=WAL;"); |
|
|
|
|
// Turn off Synchronous mode. This means that writes aren't |
|
|
|
|
// sync'd to disk every single time. |
|
|
|
|
ExecutePragma(conn,"PRAGMA synchronous=NORMAL;"); |
|
|
|
|
// Increate the cache page size TODO: check this value |
|
|
|
|
//ExecutePragma("PRAGMA cache_size=10000;"); |
|
|
|
|
// Use a shared cache - good for multi-threaded access |
|
|
|
|
//ExecutePragma(conn,"PRAGMA cache=shared;"); |
|
|
|
|
// Allow reading from the cache. Means we might get stale |
|
|
|
|
// data, but in most cases that's fine and concurrency will |
|
|
|
|
// be improved. |
|
|
|
|
//ExecutePragma(db, "PRAGMA read_uncommitted=true;"); |
|
|
|
|
// Store temporary tables in memory |
|
|
|
|
//ExecutePragma("PRAGMA temp_store=MEMORY;"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Database.ExecuteSqlRaw("VACUUM;"); |
|
|
|
|
} |
|
|
|
|
} |