8

我无法对特定场景进行迁移。实体框架迁移 - 支持动态连接字符串

我们的应用程序使用两种不同的模型。首先是主数据库,在那里,迁移工作正常。第二种模式适用于我们的客户数据库,这些数据库特定于每个客户(当然......),但都共享相同的模型。

因此,在应用程序(ASP.Net MVC 3)中,我们使用路线识别客户,并将所有客户托管在同一个应用程序中。因此,对于每个请求,我们将一个密钥传递给我们的客户模型dbContext构造函数,并使用该密钥通过简单的辅助方法恢复连接字符串。

应用程序工作正常,但不是迁移,因为我发现无法在程序包管理器控制台中传递密钥。

我试着创建自己的IDbConnectionFactory,但没有考虑到它。

如果我尝试连接工厂我收到以下错误,因为没有键被通过,但我可以看到我的连接工厂踢(EFCustomerModel.EFConnectionFactory),这是正常的更新的数据库命令:

PM> update-database -verbose 
Using NuGet project 'EFCustomerModel'. 
Using StartUp project 'PMEL.DatabaseSetup'. 
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0. 
    at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) 
    at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) 
    at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) 
    at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) 
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) 
    at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) 
    at System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value) 
    at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) 
    at System.Data.SqlClient.SqlConnection..ctor(String connectionString) 
    **at EFCustomerModel.EFConnectionFactory.CreateConnection(String nameOrConnectionString) in C:\...\EFCustomerModel\EFConnectionFactory.vb:line 18** 
    at System.Data.Entity.Internal.LazyInternalConnection.Initialize() 
    at System.Data.Entity.Internal.LazyInternalConnection.get_Connection() 
    at System.Data.Entity.Internal.LazyInternalContext.get_Connection() 
    at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo) 
    at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType) 
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext) 
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration) 
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator() 
    at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 
Format of the initialization string does not conform to specification starting at index 0. 

但是当我尝试使用-Co​​nnectionString参数我的关键传球,然后我的连接工厂被忽略,我得到了同样的错误:

PM> update-database -verbose -ConnectionString:CC99999 -ConnectionProviderName:System.Data.SqlClient 
Using NuGet project 'EFCustomerModel'. 
Using StartUp project 'PMEL.DatabaseSetup'. 
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0. 
    at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) 
    at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) 
    at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) 
    at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) 
    at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) 
    at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) 
    at System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value) 
    at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) 
    at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection) 
    at System.Data.Entity.Internal.LazyInternalConnection.Initialize() 
    at System.Data.Entity.Internal.LazyInternalConnection.get_Connection() 
    at System.Data.Entity.Internal.LazyInternalContext.get_Connection() 
    at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo) 
    at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbConnectionInfo connectionInfo) 
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext) 
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration) 
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator() 
    at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore() 
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 
Format of the initialization string does not conform to specification starting at index 0. 

所以,这是一个不支持的情况下在迁移或有是另一种通过我的方式关键或者也许有另一种方式来生成我的连接字符串?

感谢,

+0

所以,我是失去任何希望! ;-) – jjslagace

+0

您是否设法找到解决方案? – Alireza

回答

3

你应该能够一键传递给您的自定义连接工厂与-ConnectionStringName参数。该参数将传递给CreateConnection方法。

另一种可能的解决办法是从你的代码中调用数据库迁移:

var configuration = new Configuration(); 
configuration.TargetDatabase = new DbConnectionInfo(
    "Server=MyServer;Database=MyDatabase;Trusted_Connection=True;", 
    "System.Data.SqlClient"); 

var migrator = new DbMigrator(configuration); 
migrator.Update(); 
+0

谢谢,我已经尝试过-ConnectionStringName参数,但也许我应该再次尝试使用版本5. – jjslagace

+0

Nop,这不起作用,并且DbMigrator构造函数失败,抱怨缺少默认构造函数和IdbContextFactory <>。 – Alireza