JayBeeOH

EF Master / Detail Maintenance

Dec 9th, 2016
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.53 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   EF Master / Detail Maintenance
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   30 NOV 2016
  16. '
  17. ' Description:    Demonstrate how to use the Entity Framework with Windows Forms
  18. '                 in a Master / Detail maintenance program.
  19. '
  20. '                 Documentation is at:
  21. '                   App's screen image is at: http://imgur.com/XCggdZF
  22. '                   App's Visual Basic .NET code is at: http://pastebin.com/F77mRDAW
  23. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  24. '
  25. '                 Steps:
  26. '                    1. Create WinForm Project.
  27. '                    2. Add ObservableListSource class
  28. '                    3. Add ADO.NET Enitity Data Model for Entity Framework 6. Then
  29. '                          Modify "...Model.tt"
  30. '                    4. Replace first two occurrences of ICollection with ObservableListSource
  31. '                    5. Replace first occurrence of HashSet with ObservableListSource
  32. '                    6. Save and build.
  33. '
  34. '                 For additional information see ...
  35. '                     "Entity Framework Databinding with WinForms"
  36. '                     https://msdn.microsoft.com/en-us/library/jj682076(v=vs.113).aspx
  37. '
  38. '                     "Entity Framework Databinding with WPF"
  39. '                     https://msdn.microsoft.com/en-us/library/jj574514(v=vs.113).aspx
  40. '
  41. '                 The navigation of the master to detail was modified to use
  42. '                 ObservableListSource class instead of ICollection.
  43. '
  44. '-------------------------------------------------------------------------------------------
  45.  
  46. Imports System.ComponentModel
  47. Imports System.Data.Entity
  48.  
  49. Public Class MainForm
  50.  
  51.     ' Declare DbContext
  52.     Private ctx As SampleDBEntities
  53.  
  54.     ' Do intial housekeeping activites and get the data before displaying the form.
  55.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  56.         Handles MyBase.Load
  57.  
  58.         ' Make DataGridView ColumnHeader Bold
  59.         OrdersDataGridView.ColumnHeadersDefaultCellStyle.Font =
  60.             New Font(OrdersDataGridView.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
  61.  
  62.         LoadAndDisplay()
  63.     End Sub
  64.  
  65.     Private Sub LoadAndDisplay()
  66.  
  67.         If ctx IsNot Nothing Then
  68.             ctx.Dispose()
  69.         End If
  70.  
  71.         ' Instantiate DbContext
  72.         ctx = New SampleDBEntities
  73.  
  74.         ' Load master table into memory, the details table will be loaded by navigation properties
  75.         ctx.Customers.Load
  76.  
  77.         ' Alternative uses of LINQ to sort or filter data presented.
  78.  
  79.         'ctx.Customers.OrderBy(Function(c) c.CustomerName).Load
  80.         'ctx.Customers.Where(Function(c) c.CustomerName.StartsWith("T")).Load
  81.  
  82.         CustomerBindingSource.DataSource = ctx.Customers.Local
  83.  
  84.         CustomerBindingSource.MoveFirst()
  85.     End Sub
  86.  
  87.     Private Sub CustomerBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
  88.         Handles CustomerBindingNavigatorSaveItem.Click
  89.  
  90.         'TODO: Add Try/Catch for Errors.
  91.         If Validate() Then
  92.  
  93.             CustomerBindingSource.EndEdit()
  94.             OrdersBindingSource.EndEdit()
  95.  
  96.             For Each order In ctx.Orders.Local.ToList
  97.                 If order.Customer Is Nothing Then
  98.                     ctx.Orders.Remove(order)
  99.                 End If
  100.             Next
  101.  
  102.             ctx.SaveChanges()
  103.  
  104.             LoadAndDisplay()
  105.         End If
  106.     End Sub
  107.  
  108.     ' Commit the master table records changes, when enter datagridview.
  109.     Private Sub OrdersDataGridView_Enter(sender As Object, e As EventArgs) _
  110.         Handles OrdersDataGridView.Enter
  111.  
  112.         CustomerBindingSource.EndEdit()
  113.     End Sub
  114.  
  115.     ' Upon closing out the form, dispose of the DbContext.
  116.     Private Sub MainForm_Closing(sender As Object, e As CancelEventArgs) _
  117.         Handles Me.Closing
  118.  
  119.         'TODO: Add check for DBContext is dirty before exit.
  120.  
  121.         If ctx IsNot Nothing Then
  122.             ctx.Dispose()
  123.         End If
  124.     End Sub
  125.  
  126. End Class
  127.  
  128. '------------------------------------------------------------------------------------------
  129. '           Notice of My Copyright and Intellectual Property Rights
  130. '
  131. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  132. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  133. ' publish or provide such intellectual property to any other person or entity for any
  134. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  135. '
  136. '                 Copyright © 2016. All rights reserved.
  137. '        All trademarks remain the property of their respective owners.
  138. '-------------------------------------------------------------------------------------------
  139. ' CLass Name:     ObservableListSouce
  140. '
  141. ' VBnet Author:   Joseph L. Bolen
  142. ' Date Created:   30 NOV 2016
  143. '
  144. ' Description:    Class allow for observability and sorting in WinForm databinding
  145. '                 in programs.
  146. '
  147. '                 Documentation is at:
  148. '                   App's screen image is at: http://imgur.com/XCggdZF
  149. '                   App's Visual Basic .NET code is at: http://pastebin.com/F77mRDAW
  150. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  151. '
  152. '                 Steps:
  153. '                    1. Create WinForm Project.
  154. '                    2. Add ObservableListSource class (This class.)
  155. '                    3. Add ADO.NET Enitity Data Model for Entity Framework 6. Then
  156. '                          Modify "...Model.tt"
  157. '                    4. Replace first two occurrences of ICollection with ObservableListSource
  158. '                    5. Replace first occurrence of HashSet with ObservableListSource
  159. '                    6. Save and build.
  160. '
  161. '                 For additional information see ...
  162. '                     "Entity Framework Databinding with WinForms"
  163. '                     https://msdn.microsoft.com/en-us/library/jj682076(v=vs.113).aspx
  164. '
  165. '-------------------------------------------------------------------------------------------
  166.  
  167. Imports System.Collections.ObjectModel
  168. Imports System.ComponentModel
  169. Imports System.Data.Entity
  170.  
  171. Public Class ObservableListSource(Of T As Class)
  172.     Inherits ObservableCollection(Of T)
  173.     Implements IListSource
  174.  
  175.     Private _bindingList As IBindingList
  176.  
  177.     Private ReadOnly Property IListSource_ContainsListCollection() As Boolean Implements IListSource.ContainsListCollection
  178.         Get
  179.             Return False
  180.         End Get
  181.     End Property
  182.  
  183.     Private Function IListSource_GetList() As IList Implements IListSource.GetList
  184.  
  185.         If _bindingList IsNot Nothing Then
  186.             Return _bindingList
  187.         Else
  188.             _bindingList = Me.ToBindingList
  189.             Return _bindingList
  190.         End If
  191.     End Function
  192.  
  193. End Class
Advertisement
Add Comment
Please, Sign In to add comment