protected void InitializeTriggers(TestSqliteContext context) { var tables = context.Model.GetEntityTypes(); foreach (var table in tables) { var props = table.GetProperties() .Where(p => p.ClrType == typeof(byte[]) && p.ValueGenerated == Microsoft.EntityFrameworkCore.Metadata.ValueGenerated.OnAddOrUpdate && p.IsConcurrencyToken); var tableName = table.GetTableName(); foreach (var field in props) { string[] SQLs = new string[] { $@"CREATE TRIGGER IF NOT EXISTS Set{tableName}_{field.Name}OnUpdate AFTER UPDATE ON [{tableName}] FOR EACH ROW BEGIN UPDATE [{tableName}] SET [{field.Name}] = randomblob(8) WHERE rowid = NEW.rowid; END " , $@"CREATE TRIGGER IF NOT EXISTS Set{tableName}_{field.Name}OnInsert AFTER INSERT ON [{tableName}] FOR EACH ROW BEGIN UPDATE [{tableName}] SET [{field.Name}] = randomblob(8) WHERE rowid = NEW.rowid; END " }; foreach (var sql in SQLs) { using (var command = context.Database.GetDbConnection().CreateCommand()) { command.CommandText = sql; command.ExecuteNonQuery(); } } } } }
public void OneTimeSetUp() { CustomOnWrite = writeContext => { TestContext.WriteLine(writeContext); }; CustomOnBeginScope = scopeContext => { TestContext.WriteLine(scopeContext); }; ServiceCollectionInstance = new ServiceCollection(); ServiceCollectionInstance.AddTestLogger(CustomOnWrite, CustomOnBeginScope); ServiceCollectionInstance.AddSingleton <SqliteConnection>(fact => { var connStr = "Filename=:memory:"; var conn = new SqliteConnection(connStr); conn.Open(); return(conn); }); ServiceCollectionInstance.AddSingleton <DbContextOptions <TestSqliteContext> >(fact => { var conn = fact.GetRequiredService <SqliteConnection>(); var loggingFact = fact.GetRequiredService <ILoggerFactory>(); var tmp = new DbContextOptionsBuilder <TestSqliteContext>().UseSqlite(conn).UseLoggerFactory(loggingFact); tmp.EnableSensitiveDataLogging(); return(tmp.Options); }); ServiceCollectionInstance.AddTransient <TestSqliteContext>(fact => { var options = fact.GetRequiredService <DbContextOptions <TestSqliteContext> >(); var tmp = new TestSqliteContext(options); return(tmp); }); }