Some quick questions:
So I have this LogInWindow dialog that I use to collect all the SQL login info and then pass the constructed connection string to the ServiceManager.InitializeContext() method, which initializes my entities context with a specific provider connection.
It's all working fine. But I would like to initialize the LogInWindow dialog with the values that are defined in the App.config configuration file.
Is there a preferred method for doing this initial setup? i.e., should I resort to instantiating a dummy EntityContext using the default constructor just for the sheer purpose of fetching the default provider connection? Is there a "cleaner" way?
By the way, would you consider it a "safe" practice to do this type of Form.Close() invocation inside a handler to the Form.Shown event? I read on MSDN that it is not advisable to call Form.Close() inside a handler to the Form.Load event.
public partial class MainWindow : Form {
private void MainWindow_Shown(object sender, EventArgs e) {
using (var logInWindow = new LogInWindow()) {
if (logInWindow.ShowDialog(this) == DialogResult.OK) {
this.serviceManager.InitializeContext(logInWindow.ConnectionString);
} else {
this.Close();
}
}
}
}
public sealed class ServiceManager : IDisposable {
public void InitializeContext(string connectionString) {
if (this.EntityContext != null)
throw new InvalidOperationException("EntityContext cannot be initialized multiple times.");
var entityConnectionString = new EntityConnectionStringBuilder();
entityConnectionString.ProviderConnectionString = connectionString;
entityConnectionString.Provider = "System.Data.SqlClient";
entityConnectionString.Metadata = "res://*/EntityModel.EntityModel.csdl|res://*/EntityModel.EntityModel.ssdl|res://*/EntityModel.EntityModel.msl";
this.EntityContext = new EntityContext(entityConnectionString.ConnectionString);
this.EntityContext.ObjectMaterialized += EntityContext_ObjectMaterialized;
}
}