EF Code First&存储过程返回多个结果

时间:2016-09-26 12:15:12

标签: c# entity-framework

我读过这篇文章https://romiller.com/2012/08/15/code-first-stored-procedures-with-multiple-results/

并尝试实现以下代码来处理返回多个结果集的存储过程。

这些是我包含的名称空间:

"

查看我的完整代码

using System.Data;
using System.Data.SqlClient;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;

我的示例存储过程返回多个结果集

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EFTest.demo1;

namespace EFTest
{
    public partial class CallSP : Form
    {
        public CallSP()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            using (var db = new TestDBContext1())
            {
                db.Database.Initialize(force: false);
                // Create a SQL command to execute the sproc 
                var cmd = db.Database.Connection.CreateCommand();
                cmd.CommandText = "[dbo].[MultiResultSet]";

                try
                {

                    db.Database.Connection.Open();
                    // Run the sproc  
                    var reader = cmd.ExecuteReader();

                    // Read Blogs from the first result set 
                    var blogs = ((IObjectContextAdapter)db)
                        .ObjectContext
                        .Translate<Customer>(reader, "Customers", MergeOption.AppendOnly);


                    foreach (var item in blogs)
                    {
                        Console.WriteLine(item.Name);
                    }

                    // Move to second result set and read Posts 
                    reader.NextResult();
                    var posts = ((IObjectContextAdapter)db)
                        .ObjectContext
                        .Translate<Addresses>(reader, "Addresses", MergeOption.AppendOnly);


                    foreach (var item in posts)
                    {
                        Console.WriteLine(item.Title);
                    }
                }
                finally
                {
                    db.Database.Connection.Close();
                }
            }

        }
    }
}

我收到编译错误。

  

最佳重载方法匹配   &#39; System.Data.Entity.Core.Objects.ObjectContext.Translate(System.Data.Common.DbDataReader,   string,System.Data.Entity.Core.Objects.MergeOption)&#39;有一些无效的   参数

请帮我修复一下,结果应该没有编译错误。

由于

1 个答案:

答案 0 :(得分:0)

使用名称空间MergeOption中的System.Data.Entity.Core.Objects,而不是使用MergeOption中的System.Data.Objects

var blogs = ((IObjectContextAdapter)db)
                        .ObjectContext
                        .Translate<Customer>(reader, "Customers", System.Data.Entity.Core.Objects.MergeOption.AppendOnly);

var posts = ((IObjectContextAdapter)db)
                        .ObjectContext
                        .Translate<Addresses>(reader, "Addresses", System.Data.Entity.Core.Objects.MergeOption.AppendOnly);