Entity Framework 6: is there a way to iterate through a table without holding each row in memory

前端 未结 4 1203
悲&欢浪女
悲&欢浪女 2021-01-04 19:34

I would like to be able to iterate through every row in an entity table without holding every row in memory. This is a read only operation and every row can be discarded aft

4条回答
  •  借酒劲吻你
    2021-01-04 20:17

    Based on your last comment, I'm still confused. Take a look at both of below code.

    EF

    using (var ctx = new AppContext())
    {
        foreach (var order in ctx.Orders)
        {
            Console.WriteLine(order.Date);
        }
    }
    

    EF Profiler

    Data Reader

    var constr = ConfigurationManager.ConnectionStrings["AppContext"].ConnectionString;
    using (var con = new SqlConnection(constr))
    {
        con.Open();    
        var cmd = new SqlCommand("select * from dbo.Orders", con);
        var reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(reader["Date"]);
        }
    }
    

    Data Reader Profiler

    Even though EF has few initial query, both of them execute similar query that can be seen from profiler..

提交回复
热议问题