Reputation: 2451
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
Upvotes: 2
Views: 2173
Reputation: 3610
If your set doesn't have a name, you can just write
var customers = ((IObjectContextAdapter)db).ObjectContext.Translate<Customer>(reader);
The default for that MergeOption
is already AppendOnly
, so you don't have to worry about that parameter as well.
Upvotes: 3
Reputation: 12077
I don't think your entity set has a name. I think if you pass null
as the second input to Translate
, it will do what you want:
var customers = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Customer>(reader, null, MergeOption.AppendOnly);
Upvotes: 1