Guest User

Untitled

a guest
Nov 30th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. public class MyDbContext
  2. {
  3. public MyDbContext() : base(Connection(), true)
  4. {
  5. this.Configuration.AutoDetectChangesEnabled = true;
  6. }
  7.  
  8.  
  9.  
  10. public DbSet<UserRepository.User> Users { get; set; }
  11. public DbSet<UserRepository.Group> Groups { get; set; }
  12.  
  13. private static string ConnectionString = null;
  14. private static System.Data.Common.DbConnection Connection()
  15. {
  16. var con = System.Data.Common.DbProviderFactories.GetFactory("Npgsql").CreateConnection();
  17. con.ConnectionString = ConnectionString;
  18. return con;
  19. }
  20. public static bool RegisterProvider(string name, string desc, string invariant, string type)
  21. {
  22. try
  23. {
  24. var ds = System.Configuration.ConfigurationManager.GetSection("system.data") as DataSet;
  25. if (!ds.Tables[0].Rows.Cast<DataRow>().Any((row) => row["InvariantName"].ToString() == invariant))
  26. {
  27. ds.Tables[0].Rows.Add(name, desc, invariant, type);
  28. }
  29. return true;
  30. }
  31. catch
  32. {
  33. return false;
  34. }
  35. }
  36.  
  37. public static void RegisterProvider()
  38. {
  39. RegisterProvider("Npgsql Data Provider", ".Net Framework Data Provider for Postgresql", "Npgsql", "Npgsql.NpgsqlFactory, Npgsql");
  40. }
  41. public static void SetConnectionString(string host = "localhost", string username = "postgres", string password = "", string database = "postgres")
  42. {
  43. ConnectionString = new Npgsql.NpgsqlConnectionStringBuilder
  44. {
  45. Username = username,
  46. Password = password,
  47. Host = host,
  48. Database = database
  49. }.ToString();
  50. }
  51. }
  52.  
  53. Enable-Migrations -ProjectName "Project.MyDbContext"
  54. Checking if the context targets an existing database...
  55. System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.
  56. at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
  57. at Project.MyDbContext.Connection() in Z:...MyDbContext.cs:line 33
  58. at Project.MyDbContext..ctor() in Z:...MyDbContext.cs:line 16
  59. --- End of stack trace from previous location where exception was thrown ---
  60. at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  61. at System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()
  62. at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver)
  63. at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
  64. at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
  65. at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
  66. at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
  67. at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
  68. at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
  69. at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
  70. at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
  71. at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
  72. at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
  73. Unable to find the requested .Net Framework Data Provider. It may not be installed.
Add Comment
Please, Sign In to add comment