EF 4.1 Code First多个结果集

6 entity-framework ef-code-first

我需要执行返回多个结果集的Raw SQL查询.这在EF CF中可能吗?

谢谢!

dkn*_*ack 4

描述

是的你可以!;) 您也可以在 Entity Framework Code First 中使用 DbCommand 执行原始 sql 查询。

NextResult()您可以使用多个结果集执行查询,并使用该类的方法跳转到下一个结果集SqlDataReader

样本

namespace MyNamespace
{
    public class MyDbContext : DbContext
    {

    }

    public class Test
    {
        public void Test()
        {
            MyDbContext context = new MyDbContext();

            DbCommand db = context.Database.Connection.CreateCommand();
            db.CommandText = "SELECT propertie1 FROM Table1; SELECT propertie1 from Table2";
            try
            {
                DbDataReader reader = db.ExecuteReader();

                while (reader.Read())
                {
                    // read your data from result set 1
                    string value = Convert.ToString(reader["propertie1"]);
                }

                reader.NextResult();

                while (reader.Read())
                {
                    // read your data from result set 2
                    string value = Convert.ToString(reader["propertie1"]);
                }
            }
            catch
            {
                // Exception handling
            }
            finally
            {
                if (db.Connection.State != ConnectionState.Closed)
                    db.Connection.Close();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更多信息