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