Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. var dstuff = from user in selecteddb.users
  2. where user.UserEmail == userEmail
  3. select user.UserID;
  4.  
  5. public static class ConnectionTools
  6. {
  7. // all params are optional
  8. public static void ChangeDatabase(
  9. this DbContext source,
  10. string initialCatalog = "",
  11. string dataSource = "",
  12. string userId = "",
  13. string password = "",
  14. bool integratedSecuity = true,
  15. string configConnectionStringName = "")
  16. /* this would be used if the
  17. * connectionString name varied from
  18. * the base EF class name */
  19. {
  20. try
  21. {
  22. // use the const name if it's not null, otherwise
  23. // using the convention of connection string = EF contextname
  24. // grab the type name and we're done
  25. var configNameEf = string.IsNullOrEmpty(configConnectionStringName)
  26. ? source.GetType().Name
  27. : configConnectionStringName;
  28.  
  29. // add a reference to System.Configuration
  30. var entityCnxStringBuilder = new EntityConnectionStringBuilder
  31. (System.Configuration.ConfigurationManager
  32. .ConnectionStrings[configNameEf].ConnectionString);
  33.  
  34. // init the sqlbuilder with the full EF connectionstring cargo
  35. var sqlCnxStringBuilder = new SqlConnectionStringBuilder
  36. (entityCnxStringBuilder.ProviderConnectionString);
  37.  
  38. // only populate parameters with values if added
  39. if (!string.IsNullOrEmpty(initialCatalog))
  40. sqlCnxStringBuilder.InitialCatalog = initialCatalog;
  41. if (!string.IsNullOrEmpty(dataSource))
  42. sqlCnxStringBuilder.DataSource = dataSource;
  43. if (!string.IsNullOrEmpty(userId))
  44. sqlCnxStringBuilder.UserID = userId;
  45. if (!string.IsNullOrEmpty(password))
  46. sqlCnxStringBuilder.Password = password;
  47.  
  48. // set the integrated security status
  49. sqlCnxStringBuilder.IntegratedSecurity = integratedSecuity;
  50.  
  51. // now flip the properties that were changed
  52. source.Database.Connection.ConnectionString
  53. = sqlCnxStringBuilder.ConnectionString;
  54. }
  55. catch (Exception ex)
  56. {
  57. // set log item if required
  58. }
  59. }
  60. }
  61.  
  62. // assumes a connectionString name in .config of ADBEntities
  63. var selectedDb = new ADBEntities();
  64. // so only reference the changed properties
  65. // using the object parameters by name
  66. selectedDb.ChangeDatabase
  67. (
  68. initialCatalog: "name-of-bdb-initialcatalog",
  69. userId: "jackthelad",
  70. password: "nosecrets",
  71. dataSource: @".sqlexpress" // could be ip address 100.23.45.67 etc
  72. );
  73.  
  74. using (var context = new MyDbContext("Server=localhost;Database=dbA;..."))
  75. {
  76. return context.Users.Where(u => u.Email == "someemail@google.ca").Single();
  77. }
  78.  
  79. var connections = ConfigurationManager.ConnectionStrings;
  80.  
  81. foreach (ConnectionStringSettings connection in connections)
  82. //display connection.Name
  83.  
  84. ConnectionStringSettings selected_connection = connections[1];
  85.  
  86. using (var selecteddb = new ADBEntities(selected_connection.ConnectionString))
  87. {
  88. var dstuff = from user in selecteddb.users
  89. where user.UserEmail == userEmail
  90. select user.UserID;
  91. }
  92.  
  93. var defaultString = _myContext.Database.GetDbConnection().ConnectionString;
  94. _myContext.Database.GetDbConnection().ConnectionString ="new connection" or _myContext.Database.GetDbConnection().ChangeDatabase()
  95. //your query..
  96. _myContext.Database.GetDbConnection().ConnectionString = defaultString;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement