Get the Entity Framework Connection String

前端 未结 11 1188
迷失自我
迷失自我 2021-01-31 02:27

We use Entity Framework 5, but have a requirement to ALSO use a normal database connection from the application for some custom SQL we need to perform.

So, I am creating

11条回答
  •  星月不相逢
    2021-01-31 03:02

    I had this problem too, to solve the problem you can do this:

            SqlConnection con = context.Database.Connection as SqlConnection;
            SqlCommand command = new SqlCommand(sql, con);
            con.Open();
    
            SqlDataReader reader = command.ExecuteReader();
            while (reader.HasRows)
            {
                while (reader.Read())
                {
                    //read your fields
                }
    
                reader.NextResult();
            }
    
            con.Close();
    

提交回复
热议问题