0

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;
    }
}

1 Answer 1

1

You can access all parts of your App.config using System.Configuration class

Example:

var connectString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

var stringBuilder = new SqlConnectionStringBuilder(connectString);

string UserID = stringBuilder.UserID;
string Password = stringBuilder.Password;
string catalog = stringBuilder.InitialCatalog;

App.config

<configuration>
   <connectionStrings>
     <add name="MyConnectionString" connectionString="metadata=res://*/...........
   </connectionStrings>
</configuration>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Now I'm curious, what type of connection does the ConfigurationManager.ConnectionStrings collection return? A EntityConnection? Keep in mind I'm only interested in the provider connection string part of it.
Oh, I see, it's a ConnectionStringSettings... Problem is, that type does not break the connection string into parts, i.e., the Initial Catalog, Uset ID, Password, etc... I still find using the ConfigurationManager useful though.
I have an application that does exactly what you are tying to do, I just split the connection string to get the Username/password. There could be a nicer way to do this, but I have found no problems with the hacky way I am doing it :)
There is a way to do what you want, fixes my implementation too :) Updated above
I realize that this is an old answer, but thought it would be helpful for others to know that I've found it not to work with current versions of ADO.Net. When I call the SqlConnectionStringBuilder() constructor, it fails with an exception with the message "Keyword not supported: 'metadata'." My connection string is a standard EF connection string - ` <connectionStrings><add name="Development" connectionString="metadata=res://*/BModel.csdl|res://*/BModel.msl;provider=System.Data.SqlClient;provider connection string='data s...." providerName="System.Data.EntityClient"/></connectionStrings>`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.