我有以下问题。我有一个项目A
(Windows窗体)和项目B
(MVC应用程序)。在项目A
中,我引用了项目B
,因为我想使用它中的一些类。有一次,我必须从项目B
获得修改后的连接字符串,并连接到项目A
中的数据库。
项目B
中的代码,即操纵连接字符串:
public static string ConnectionString = "";
public TheClassThatManipulateTheConnectionString (){
ConnectionString = ManageConnectionString();
}
private string ManageConnectionString(){
WriteToFile.WriteToTextFile("0");
string ConfigurationString = ConfigurationManager.ConnectionStrings["DbConnectionString"].ConnectionString;
WriteToFile.WriteToTextFile("1");
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(ConfigurationString);
WriteToFile.WriteToTextFile("2");
DbProviderFactory factory = DbProviderFactories.GetFactory(entityBuilder.Provider);
WriteToFile.WriteToTextFile("3");
DbConnectionStringBuilder providerBuilder = factory.CreateConnectionStringBuilder();
WriteToFile.WriteToTextFile("4");
providerBuilder.ConnectionString = entityBuilder.ProviderConnectionString;
WriteToFile.WriteToTextFile("5");
providerBuilder.Add("Password", "ThePassword");
WriteToFile.WriteToTextFile("6");
entityBuilder.ProviderConnectionString = providerBuilder.ToString();
WriteToFile.WriteToTextFile("7");
return entityBuilder.ToString();
}
项目A
中的代码,即调用Project B
方法:
public Form1(){
InitializeComponent();
ProjectB.Folder.Folder.TheClassThatManipulateTheConnectionString TheClassThatManipulateTheConnectionString = new ProjectB.Folder.Folder.TheClassThatManipulateTheConnectionString ();
using (ProjectB.Folder.Folder.Context db = new ProjectB.Folder.Folder.Context TheClassThatManipulateTheConnectionString.TheConnectionString))
{ }
}
Exceoption与track code
一起写入文件。
'08/06/2014 21:29:35: 0
08/06/2014 21:29:35: Exception: System.NullReferenceException: Object reference not set to an instance of an object.'
项目A的例外
at System.Data.Entity.Utilities.Check.NotEmpty(String value, String parameterName)
at System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
at ProjectB.Folder. Folder.Context..ctor(String ConnectionString) in c:\Users\Administrator\Documents\Visual Studio 2013\Projects\ ProjectB \ ProjectB \ Folder \ Folder \DBEntities.Context.cs:line 20
at ProjectA.Form1.FillUsers() in c:\Users\Administrator\Documents\Visual Studio 2013\Projects\ ProjectA\ ProjectA \Form1.cs:line 51
在文字文件中,我可以看到0
,但不是1
。我的问题是,当将配置管理器调用到项目A中时,为什么配置管理器无法正常工作。