public void Add(Document document, IFormFile file, string path, Action <IFormFile, string> copyToDisc) { _logger.LogDebug($"Add to database file={file}"); using (AnyDbConnection connection = _factory.OpenConnection()) using (DbTransaction transaction = connection.BeginTransaction()) { try { connection.Insert(document, transaction: transaction); copyToDisc(file, path); transaction.Commit(); } catch { transaction.Rollback(); throw; } } _logger.LogDebug($"FileDbService.Add .... OK"); }
void InitDb() { if (_factory.Provider == DatabaseProvider.PostgreSql) { PgDbSetting ps = new PgDbSetting(); Npgsql.NpgsqlConnectionStringBuilder sb = new Npgsql.NpgsqlConnectionStringBuilder(ps.ConnectionString); sb.Database = "postgres"; ps.ConnectionString = sb.ToString(); AnyDbFactory f = new AnyDbFactory(ps); using (AnyDbConnection con = f.OpenConnection()) { con.Execute("drop database if exists qb_test"); con.Execute("create database qb_test"); } } if (_factory.Provider == DatabaseProvider.SqLite) { Microsoft.Data.Sqlite.SqliteConnectionStringBuilder sb = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder(_factory.ConnectionString); if (File.Exists(sb.DataSource)) { File.Delete(sb.DataSource); } } using (AnyDbConnection con = _factory.OpenConnection()) { con.Execute(@" CREATE TABLE customer ( id serial NOT NULL, first_name varchar(50) NULL, last_name varchar(50) NULL, CONSTRAINT customer_pk PRIMARY KEY (id) );"); for (int i = 0; i < 100; i++) { con.Execute($"insert into customer (first_name, last_name) values ('F_{i}', 'L_{i}')"); } } }