2012-12-15 30 views
3

我有一個由4個項目組成的解決方案。 MVC,WCF,Business LYR,DataAcess。我正在使用數據庫事務的實體框架。我的要求是,我只想從MVC webconfig獲取實體連接字符串,而不引用接口層的APP.cofig。在這種情況下可能嗎?來自另一個項目的實體框架連接字符串引用

雖然我嘗試了下面的代碼,但是出現了一個錯誤。

this.ConnectionString="data source=cmh-sosql;initial catalog=Student;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"; 

      System.Data.SqlClient.SqlConnectionStringBuilder scsb = new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString); 

EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder(); 
ecb.Metadata = "res://*/schoolModel.csdl|res://*/schoolModel.ssdl|res://*/schoolModel.msl"; 
ecb.Provider = "System.Data.SqlClient"; 
ecb.ProviderConnectionString = scsb.ConnectionString; 

using (SchoolDB schoolDB = new SchoolDB(ecb.ConnectionString)) 

錯誤:實體類型學生不是當前上下文的模型的一部分。

+0

你能解釋一下爲什麼這應該需要?即不從App.config? – Sampath

+0

我需要從第一個連接的數據庫中獲取第二個連接。這是我的問題...現在解決了... –

回答

2

你是絕對正確的。我得到了解決方案。不需要在webconfig中保留任何字符串以引用實體模型。我們可以使用上面的代碼來引用它。但更改是配置上下文對象。

public SchoolDB(string connectionString) 
     : base(connectionString) 
    { 
    } 
We need to change the constructor also by this format. 

感謝薩姆帕斯

+0

很高興爲您提供幫助。但請接受您的答案作爲解決方案。然後其他人可以快速找到解決方案。 – Sampath

相關問題