/// <summary> /// 增加 /// </summary> /// <param name="entity">系统数据</param> public void Insert(UserModel entity) { using (var conn = new Npgsql.NpgsqlConnection(ConnectionString)) { conn.Insert(entity); } }
protected int?Insert <TEntity>(TEntity entity, IDbTransaction transaction = null, int?commandTimeout = null) where TEntity : class { using (IDbConnection connection = new Npgsql.NpgsqlConnection(this.connectionString)) { try { connection.Open(); return((int?)connection.Insert(entity, transaction, commandTimeout)); } catch (Exception exc) { Console.WriteLine(exc.Message); throw exc; } } }
public void PgSql() { PgSqlCommandExecutor.Configure(); using (var connection = new Npgsql.NpgsqlConnection("host=localhost;username=postgres;password=Norther;database=Test")) { connection.Open(); using (var transaction = connection.BeginTransaction()) { var testData = new TestTable() { Foo = "Foo", Name = "Hello", TheDate = DateTime.Now }; TestTable result; using (Time(elapsed => Console.WriteLine("Insert : " + elapsed))) { result = connection.Insert(testData, transaction); } Console.WriteLine("Database generated UID : " + result.TheSecondId); List<TestTable> bulkResult; using (Time(elapsed => Console.WriteLine("Bulk insert : " + elapsed))) { bulkResult = connection.Insert(Enumerable.Range(0, 100).Select(i => new TestTable() { Name = i.ToString() }), transaction).ToList(); Assert.AreEqual(100, bulkResult.Count()); } using (Time(elapsed => Console.WriteLine("Bulk update : " + elapsed))) { bulkResult = connection.Update( bulkResult.Select(item => item.SetProperties(new TestTable() { Name = item.Name + " strongly typed mutation" })), transaction).ToList(); } using (Time(elapsed => Console.WriteLine("Update : " + elapsed))) { var newDate = DateTime.Now.AddYears(-50); var update = connection.Update( result.SetProperties( new { TheDate = newDate } ), transaction ); Assert.AreEqual(newDate.Year, update.TheDate.Year); } using (Time(elapsed => Console.WriteLine("FindById : " + elapsed))) { Assert.IsNotNull(connection.FindById<TestTable>(new { result.Id, result.TheSecondId }, transaction)); } transaction.Commit(); } } }