add ef core

master
ygl 3 years ago
parent 40834d3643
commit fde07519c3
  1. 76
      realm_cli/AppDbContext.cs
  2. 11
      realm_cli/LiveDataState.cs
  3. 28
      realm_cli/Program.cs
  4. 1
      realm_cli/realm_cli.csproj

@ -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;");
}
}

@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations.Schema;
using Realms;
namespace realm_cli;
public class LiveDataState
{
public int ItemId { get; set; }
public ushort EcuId { get; set; }
public float Value { get; set; }
}

@ -3,6 +3,7 @@
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Data.Sqlite;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Serializers;
using realm_cli;
@ -214,9 +215,30 @@ using (var db = GetDB())
//var t1 = Task.Run(() => Produce());
//var t2 = Task.Run(() => Consume());
var t1 = ProduceAsync();
var t2 = ConsumeAsync();
Task.WaitAll(t1, t2);
//var t1 = ProduceAsync();
//var t2 = ConsumeAsync();
//Task.WaitAll(t1, t2);
var b = DateTime.Now;
using (var ctx = new AppDbContext())
{
for (var i = 1; i != 1000; ++i)
{
//{
var s = new LiveDataState {ItemId = i, EcuId = (ushort) (i + 1), Value = (float) (i * 1.0)};
ctx.Add(s);
//Console.WriteLine(ctx.Entry<LiveDataState>(s).Entity.ItemId);
//ctx.SaveChanges();
//}
}
ctx.SaveChanges();
}
Console.WriteLine($"{(DateTime.Now - b).TotalSeconds}");
/*
var copy_obj = await QueryLiveData();

@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.17" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
<PackageReference Include="Realm" Version="10.18.0" />
</ItemGroup>

Loading…
Cancel
Save