[英]EF Code First & Stored Procedures return Multiple Results
我閱讀了這篇文章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)'的最佳重載方法匹配具有一些無效的參數
請幫助我解決什么問題,因此應該沒有編譯錯誤。
謝謝
使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.