【问题标题】:EF Code First & Stored Procedures return Multiple ResultsEF 代码优先和存储过程返回多个结果
【发布时间】:2016-11-19 16:20:18
【问题描述】:

我看了这篇文章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();
                }
            }

        }
    }
}

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

CREATE PROCEDURE [dbo].[MultiResultSet]
AS
    SELECT * FROM Customers

    SELECT * FROM Addresses

我收到编译错误。

最好的重载方法匹配 'System.Data.Entity.Core.Objects.ObjectContext.Translate(System.Data.Common.DbDataReader, string, System.Data.Entity.Core.Objects.MergeOption)' 有一些无效 论据

请帮我解决什么问题,结果应该没有编译错误。

谢谢

【问题讨论】:

  • 您使用的是 EF5 吗?

标签: c# entity-framework


【解决方案1】:

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

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-26
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    相关资源
    最近更新 更多