I have an application that uses EF6. I need to execute the stored procedure, so I do the following:
using (SqlConnection conn = new SqlConnection(context.Database.Connection.ConnectionString))
{
SqlCommand cmd = new SqlCommand("dbo.ExampleStoredProcedure", conn)
{
CommandType = CommandType.StoredProcedure
};
cmd.Parameters.Add(new SqlParameter("@param1", "parameterValue"));
SqlDataAdapter da = new SqlDataAdapter { SelectCommand = cmd };
durations = new DataSet();
da.Fill(durations);
}
My connection string from Web.config is:
<add name="MyEntities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="data source=MyServer\DEV;initial catalog=MyApp;User Id=UserId;Password=password123;Application Name=Connection Secured;integrated security=false;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
When the code above runs I get an exception:
Login failed for user 'UserId'.
The exception also contains the SQL error code: 18456. I confirmed that user has full access to this SQL server, database, instance and stored procedure by logging into SQL server with this SQL account and running the stored procedure in question.
I also tried defining a new SqlConnection object:
SqlConnection newCon = new SqlConnection("Server=MyServer\\DEV;Database=MyApp;User Id=UserId;Password = password123;");
and executing the stored procedure against it - it worked this time. So I do have a workaround, but would like to use the context.Database.Connection.ConnectionString object to avoid having multiple connection string in web.config file.
Any suggestions?