2

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94

2 Answers2

3

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.

Tys
  • 3,592
  • 9
  • 49
  • 71
1

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);
Daniel Pratt
  • 12,007
  • 2
  • 44
  • 61
  • 1
    sorry i set null but no benefit and got a error msg `The argument 'entitySetName' cannot be null, empty or contain only white space.` – Monojit Sarkar Sep 26 '16 at 13:07
  • is there any way to see entity set name from code ? – Monojit Sarkar Sep 26 '16 at 13:07
  • 1
    Ah, you're right, sorry. It's annoying because I can see that internally a null parameter would be handled fine if the null check were removed. You might take a look at [this answer](http://stackoverflow.com/a/3893502/76123). I think it may give you some ideas about how to see the entity set names. – Daniel Pratt Sep 26 '16 at 13:34