public void Delete(int id, uint?oid) { if (!oid.HasValue) { oid = _connection.Images.Where(info => info.Id == id).Select(info => info.ImageId).SingleOrDefault(); } using (var transaction = _connection.BeginTransaction()) { _largeObjectManager.Unlink(oid.Value); _connection.Images.Where(info => info.Id == id).Delete(); transaction.Commit(); } }
public void Test() { using (var conn = OpenConnection()) using (var transaction = conn.BeginTransaction()) { var manager = new NpgsqlLargeObjectManager(conn); uint oid = manager.Create(); using (var stream = manager.OpenReadWrite(oid)) { var buf = Encoding.UTF8.GetBytes("Hello"); stream.Write(buf, 0, buf.Length); stream.Seek(0, System.IO.SeekOrigin.Begin); var buf2 = new byte[buf.Length]; stream.Read(buf2, 0, buf2.Length); Assert.That(buf.SequenceEqual(buf2)); Assert.AreEqual(5, stream.Position); Assert.AreEqual(5, stream.Length); stream.Seek(-1, System.IO.SeekOrigin.Current); Assert.AreEqual((int)'o', stream.ReadByte()); manager.MaxTransferBlockSize = 3; stream.Write(buf, 0, buf.Length); stream.Seek(-5, System.IO.SeekOrigin.End); var buf3 = new byte[100]; Assert.AreEqual(5, stream.Read(buf3, 0, 100)); Assert.That(buf.SequenceEqual(buf3.Take(5))); stream.SetLength(43); Assert.AreEqual(43, stream.Length); } manager.Unlink(oid); transaction.Rollback(); } }
/// <summary> /// Deletes blob from DB and removes record from files table /// </summary> /// <param name="context"></param> public void Delete(DbContext context) { var connection = context.Database.GetDbConnection(); if (connection is NpgsqlConnection) { var conn = (NpgsqlConnection)connection; conn.Open(); using (var transaction = conn.BeginTransaction()) { var blobManager = new NpgsqlLargeObjectManager(conn); blobManager.Unlink(BlobOid); transaction.Commit(); } conn.Close(); context.Remove(this); context.SaveChanges(); } else { throw new NotSupportedException("Unsupported database adapter."); } }