2

I call a stored procedure from EF that deletes some records and then immediately call a query to get the remaining records. That query uses Single() in it which is throwing an error

Sequence contains no elements

But after this error, if I stop and restart the code, it shows the records just fine. So I'm thinking the deletion of the records the stored procedure is doing (it's getting committed) is messing up the EF context.

I do call dbContext.SaveChanges() after the stored procedure is run. Why is the EF DbContext having issues querying after a delete statement is ran from a stored procedure?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user441521
  • 6,942
  • 23
  • 88
  • 160

1 Answers1

1

If the query to return remaining records isn't returning anything (as in, no more records remain), then Single() is going to throw that exception because, as the error states, there are no elements in that sequence. If you use SingleOrDefault() instead, that can return a null object which you could check.

Also, this code would throw an exception if you have more than 1 element in the array of returned remaining records.

If you post some code that you are using, I'll edit this answer to possibly get more specific.

Matti Price
  • 3,351
  • 15
  • 28
  • I get I can use SingleOrDefault() but the bigger question is why is EF getting out of sync with what the tables actually look like. When I stop and start the app it runs this query just fine. Is there something about calling a stored proc that changes the data and then querying on the same instances of the db context. – user441521 Sep 26 '16 at 16:22
  • What exactly is the code you are running before `Single()` and what is the result in the database that you are expecting that EF isn't seeing before you restart? – Matti Price Sep 26 '16 at 16:24
  • EF calls a stored proc and that proc deletes records from 2 tables that I'm querying after it does that via the EF db context. – user441521 Sep 26 '16 at 16:25
  • I understand that, I'm asking for actually records that you are expecting to come back. The error from EF says there are no records, what records are you expecting to see, and what is the code that is failing to see them? – Matti Price Sep 26 '16 at 16:34
  • There are many other records in the table. I'm deleting a couple. I expect to see the other records. Which does happen if I close the app and reopen it and it runs the exact same query it runs all the time. I would expect EF to go back to the tables each time to run and if some records aren't there anymore they shouldn't cause this error (as it doesn't happen when I rerun the app). The actual EF linq query is what's failing after the deletion by the proc. Seeing that query isn't going to help because it works just fine before the deletion and after when I restart the app. – user441521 Sep 26 '16 at 16:41
  • I'm not sure if EF somehow handles it differently for you, but calling `.Single()` and it returning multiple items is going to cause an exception. You need to refresh the EF context. The records being deleted in a proc aren't going to be known to EF because, strictly speaking, it didn't delete them. Call `context.Entry(recordEntity).Reload();` – Matti Price Sep 26 '16 at 19:23