PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/WP7.1/Templates/VB/WPCloud.SQL.Mem/WindowsPhoneCloud.Web/Infrastructure/SqlSampleDataInitializer.vb

#
Visual Basic | 69 lines | 36 code | 9 blank | 24 comment | 0 complexity | 0ce643df338a7805ad7685b9b899bee2 MD5 | raw file
  1. ' ----------------------------------------------------------------------------------
  2. ' Microsoft Developer & Platform Evangelism
  3. '
  4. ' Copyright (c) Microsoft Corporation. All rights reserved.
  5. '
  6. ' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  7. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  8. ' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  9. ' ----------------------------------------------------------------------------------
  10. ' The example companies, organizations, products, domain names,
  11. ' e-mail addresses, logos, people, places, and events depicted
  12. ' herein are fictitious. No association with any real company,
  13. ' organization, product, domain name, email address, logo, person,
  14. ' places, or events is intended or should be inferred.
  15. ' ----------------------------------------------------------------------------------
  16. Imports System.Reflection
  17. Imports System.IO
  18. Imports System.Data.Entity
  19. Namespace Infrastructure
  20. ' Summary:
  21. ' Sample Entity Framework 4.1 data initializer class.
  22. ' The seed method is executed after the database is created using EF 4.1 Code-First,
  23. ' giving the developer the opportunity to insert "seed" (i.e. initial) data.
  24. ' For more information, visit the ADO.NET Entity Framework website at http://msdn.microsoft.com/data/aa937723
  25. Public Class SqlSampleDataInitializer
  26. Implements IDatabaseInitializer(Of SqlDataContext)
  27. Public Sub InitializeDatabase(ByVal context As SqlDataContext) Implements System.Data.Entity.IDatabaseInitializer(Of SqlDataContext).InitializeDatabase
  28. Dim initializer = New InnerSqlSampleDataInitializer()
  29. ' Initialize database using EF 4.1 Code-First.
  30. initializer.InitializeDatabase(context)
  31. ' Set the command timeout to 90 seconds.
  32. CType(context, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext.CommandTimeout = 90
  33. ' Create common database objects required by the ASP.NET Providers for SQL Server.
  34. Using stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Samples.WindowsPhoneCloud.Web.InstallCommon.sql")
  35. Dim installCommonSqlCommands = New StreamReader(stream).ReadToEnd().Split({";"c}, StringSplitOptions.RemoveEmptyEntries)
  36. For Each sqlCommand In installCommonSqlCommands
  37. If Not String.IsNullOrWhiteSpace(sqlCommand) Then
  38. context.Database.ExecuteSqlCommand(sqlCommand)
  39. End If
  40. Next sqlCommand
  41. End Using
  42. ' Create database objects specifically required by the ASP.NET Membership Provider for SQL Server.
  43. Using stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Microsoft.Samples.WindowsPhoneCloud.Web.InstallMembership.sql")
  44. Dim installMembershipSqlCommands = New StreamReader(stream).ReadToEnd().Split({";"c}, StringSplitOptions.RemoveEmptyEntries)
  45. For Each sqlCommand In installMembershipSqlCommands
  46. If Not String.IsNullOrWhiteSpace(sqlCommand) Then
  47. context.Database.ExecuteSqlCommand(sqlCommand)
  48. End If
  49. Next sqlCommand
  50. End Using
  51. End Sub
  52. Private Class InnerSqlSampleDataInitializer
  53. Inherits DropCreateDatabaseIfModelChanges(Of SqlDataContext)
  54. Protected Overrides Sub Seed(ByVal context As SqlDataContext)
  55. context.AddSampleData(1, "I am the first title", True, Date.UtcNow)
  56. context.AddSampleData(2, "I am the second title", True, Date.UtcNow)
  57. End Sub
  58. End Class
  59. End Class
  60. End Namespace