The requirement is a Windows Form Application which takes the Server, Database, User ID and Password in a Windows Form and changes the EF connection string accordingly so the project can be used in other computers without changing the code.
After research i've learned there is a way to pass a connection string to EF constructor and i've written this function.
public NorthwindEntities(string connectionString)
: base(connectionString)
{
}
After that i'm assuming i should be able to simply take the values from form and use them. I am trying to take the values as in the example below.
private void button1_Click(object sender, EventArgs e)
{
string connectionString = String.Format("metadata=res://*/Model1.csdl|" +
"res://*/Model1.ssdl|" +
"res://*/Model1.msl;" +
"provider=System.Data.SqlClient;" +
"provider connection string="" +
"data source={0};" +
"initial catalog={1};" +
"user id={2};" +
"password={3}",
cbo_server.Text, textBox1.Text, textBox2.Text, textBox3.Text);
try
{
NorthwindEntities obj = new NorthwindEntities(connectionString);
if (obj.Database.Exists())
{
//Do something
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Because EF connection string has metadata pre-fix, i tried to add it manually. However, i'm getting the error:
Keyword not supported: 'data source'.
As you may have noticed i'm new to EF so i don't have wide understanding. I just think there's more to that.