' Collection of code snippets by Arne Vajhøj 
' (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
﻿Imports System
Imports System.Linq
Imports System.Transactions

Namespace Global.Tx.NonXADB.EF
	Public Class Smart
		Inherits DBI
		Public Overrides Function GetDbContext() As MyDbContext
			Return New MyDbContext("MYSQL")
		End Function
		Public Overrides Sub Test()
			For i As Integer = 0 To 9
				Using tx As New TransactionScope()
					Using ctx As MyDbContext = GetDbContext()
						Try
							Dim osrc As TblSrc = ctx.TblSrc.First(Function(row) row.Id = 1)
							osrc.Val = osrc.Val - 1
							ctx.SaveChanges()
							If i Mod 2 <> 0 Then
								Throw New Problem()
							End If
							Dim otrg As TblTrg = ctx.TblTrg.First(Function(row) row.Id = 1)
							otrg.Val = otrg.Val + 1
							ctx.SaveChanges()
							tx.Complete()
						Catch ex As Problem
							Console.WriteLine("Problem")
						End Try
					End Using
				End Using
			Next
		End Sub
		Public Shared Sub Main(args As String())
			Demo(New Smart())
			Console.ReadKey()
		End Sub
	End Class
End Namespace
