Imports Telerik.OpenAccess

Module Module1

    Sub Main()
        Using scope As IObjectScope = ObjectScopeProvider1.ObjectScope()
            scope.Transaction.Begin()

            ' add a new customer
            Dim newCustomer As New Customer()
            newCustomer.CustomerID = "A" + System.DateTime.Now.ToString("hmsfff")
            newCustomer.CompanyName = "New Customer"
            scope.Add(newCustomer)
            Console.WriteLine("Added New Customer")

            ' get first customer using LINQ and modify
            Dim firstCustomer As Customer = _
      scope.Extent(Of Customer)().Take(1).[Single]()
            firstCustomer.CompanyName = _
      "Company " + DateTime.Now.ToString("hmsfff")
            scope.Add(firstCustomer)
            Console.WriteLine("Modified First Customer")

            ' use LINQ to retrieve customer ID's starting with 'E'
            Dim results = From c In scope.Extent(Of Customer)() _
               Where c.CompanyName.StartsWith("E") _
               Select c
            Console.WriteLine("Retreived all customers starting with E")

            ' delete 'E' customers from the database
            For Each cust As Customer In results
                For Each order As Order In cust.Orders
                    For Each detail As OrderDetail In order.OrderDetails
                        scope.Remove(detail)
                    Next
                    scope.Remove(order)
                Next
                scope.Remove(cust)
            Next

            Console.WriteLine("Removed All Customers Startingw tih E")
            scope.Transaction.Commit()
        End Using

        Console.Write("Done")
        Console.ReadLine()
    End Sub

End Module
