1

I think that Entity Framework does not have a good support for stored procedure. I would like to have a better stored procedure mapper.

The problem is : my stored procedure have an IF statement. Inside the IF, I'm doing the SELECT. When my code does not get inside the IF, the stored procedure does not select data. When I don't select data, Entity Framework's autogenerated code crashes.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Frédéric Fect
  • 213
  • 3
  • 10
  • 1
    Entity Framework does have good support for stored procedure. What are you trying to map ? Brief it . – Rangesh Feb 03 '17 at 00:59
  • My stored procedure have a IF statement. Inside the IF i'm doing the SELECT. When my code does not get inside the IF the stored proc does not select data. When I don't select data the entity framework autogenerated code crashes. – Frédéric Fect Feb 03 '17 at 01:04

1 Answers1

3

The problem is : My stored procedure have a IF statement. Inside the IF i'm doing the SELECT. When my code does not get inside the IF the stored proc does not select data. When I don't select data the entity framework autogenerated code crashes.

EF crashes because it is expecting a resultset of type x (whatever is your type), but it gets nothing if your stored procedure does not pass your if condition. The way to solve this is easy: Return an empty resultset which has the exact same number of columns from your stored procedure when the condition does not pass. Every branch of your stored procedure should return the same resultset or return an error but then you need to handle the error on your application side.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • You're being very kind to EF. In code, we might hope that a method returning a collection returns [an empty collection](http://stackoverflow.com/questions/1969993/is-it-better-to-return-null-or-empty-collection) rather than null. But we should probably not count on it. – bbsimonbb Feb 03 '17 at 08:37