The following code is supposed to run the sys.sp_setapprole stored procedure in my database. It does not. As the code sits right now I’m getting a System.InvalidOperationException with the following inner exception The FunctionImport 'sp_setapprole' could not be found in the container 'XXX'.
Any thoughts how I can execute this stored procedure ?
partial void OnContextCreated()
{
this.Connection.StateChange += (sender, args) =>
{
if (args.CurrentState == ConnectionState.Open)
{
var conn = (EntityConnection)sender;
EntityCommand cmd = conn.CreateCommand();
cmd.CommandText = "XXX.sp_setapprole";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("rolename", "XXX");
cmd.Parameters.AddWithValue("password", "XXX");
cmd.ExecuteScalar();
}
};
}
Update 1 – Using SqlConnection and SqlCommand causes an InvalidCastException, “Unable to cast object of type 'System.Data.EntityClient.EntityConnection' to type 'System.Data.SqlClient.SqlConnection'.”
Solution found here: