我建立下一個結構的WCF服務應用程序:如何使用EF6更改dbContext上的連接字符串?
WCF Client -> WCF Service -> Business Layer -> Repository -> EF6
我的應用程序需要該服務收聽到多個客戶端,每個至極的可以要求來自不同數據庫(同一型號)的數據。因此,客戶端使用Connect Method來告訴服務器需要使用哪個數據庫。 爲此,我使用創建連接字符串的類。我的問題是如何告訴上下文來改變它的連接字符串。到目前爲止,我管理的覆蓋這樣的構造函數來解決此問題:
public partial class OhmioNETEntities : DbContext
{
public OhmioNETEntities() : base(SingleConnection.ConString)
{
}
}
這工作,但問題是,如果我刷新模式,這個碼是
public partial class OhmioNETEntities : DbContext
{
public OhmioNETEntities() : base("name=OhmioNETEntities")
{
}
}
丟失和更換有這樣做的最好方法? 謝謝!
編輯 感謝所有的幫助,但我不能得到它的工作。 作爲建議我添加新的文件與此代碼:
public partial class OhmioNETEntities : DbContext
{
public OhmioNETEntities(string ConString)
{
}
}
,當我創建上下文我使用:
context = new OhmioNETEntities(SingleConnection.ConString);
,並得到這個錯誤:
{"The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715 "}
任何想法,爲什麼?謝謝!
您可以編輯model.tt模板文件進行更改生成的代碼,每次持續。 –