I am getting this error
Additional information: The EntitySet name 'TestDBContext1.Customers' could not be found.
I am trying to call a SQL Server stored procedure which returns multiple result sets.
This is my full code:
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 stored procedure
var cmd = db.Database.Connection.CreateCommand();
cmd.CommandText = "[dbo].[MultiResultSet]";
try
{
db.Database.Connection.Open();
// Run the stored procedure
var reader = cmd.ExecuteReader();
// Read Blogs from the first result set
var customers = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Customer>(reader, "Customers", MergeOption.AppendOnly);
foreach (var item in customers)
{
Console.WriteLine(item.FirstName);
}
// Move to second result set and read Posts
reader.NextResult();
var Addresses = ((IObjectContextAdapter) db)
.ObjectContext
.Translate<Addresses>(reader, "Addresses", MergeOption.AppendOnly);
foreach (var item in Addresses)
{
Console.WriteLine(item.Address1);
}
}
finally
{
db.Database.Connection.Close();
}
}
}
This line throws the above error:
var customers = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Customer>(reader, "Customers", MergeOption.AppendOnly);
My database tables are Customers
and Addresses
. Why am I getting this error? What to change in my code?
The interesting things is when I code like the below way then all works fine.
var customers = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Customer>(reader);
The moment I remove "Customers", MergeOption.AppendOnly
from code then code works fine. just do not understand what was wrong in my above code. please some one help me to understand this. Thanks