Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyDbContext
- {
- public MyDbContext() : base(Connection(), true)
- {
- this.Configuration.AutoDetectChangesEnabled = true;
- }
- public DbSet<UserRepository.User> Users { get; set; }
- public DbSet<UserRepository.Group> Groups { get; set; }
- private static string ConnectionString = null;
- private static System.Data.Common.DbConnection Connection()
- {
- var con = System.Data.Common.DbProviderFactories.GetFactory("Npgsql").CreateConnection();
- con.ConnectionString = ConnectionString;
- return con;
- }
- public static bool RegisterProvider(string name, string desc, string invariant, string type)
- {
- try
- {
- var ds = System.Configuration.ConfigurationManager.GetSection("system.data") as DataSet;
- if (!ds.Tables[0].Rows.Cast<DataRow>().Any((row) => row["InvariantName"].ToString() == invariant))
- {
- ds.Tables[0].Rows.Add(name, desc, invariant, type);
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- public static void RegisterProvider()
- {
- RegisterProvider("Npgsql Data Provider", ".Net Framework Data Provider for Postgresql", "Npgsql", "Npgsql.NpgsqlFactory, Npgsql");
- }
- public static void SetConnectionString(string host = "localhost", string username = "postgres", string password = "", string database = "postgres")
- {
- ConnectionString = new Npgsql.NpgsqlConnectionStringBuilder
- {
- Username = username,
- Password = password,
- Host = host,
- Database = database
- }.ToString();
- }
- }
- Enable-Migrations -ProjectName "Project.MyDbContext"
- Checking if the context targets an existing database...
- System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.
- at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
- at Project.MyDbContext.Connection() in Z:...MyDbContext.cs:line 33
- at Project.MyDbContext..ctor() in Z:...MyDbContext.cs:line 16
- --- End of stack trace from previous location where exception was thrown ---
- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
- at System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()
- at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver)
- at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
- at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
- at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
- at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
- at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
- at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
- at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
- at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
- at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
- at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
- Unable to find the requested .Net Framework Data Provider. It may not be installed.
Add Comment
Please, Sign In to add comment