Stored procedures and entities RRS feed

  • Question

  • I’m currently going through my first experience with EF converting a Linq to SQL project to a EF project using .NET 4.  In Linq to SQL I was able to import stored procedures and set their return types to an entity but it didn’t matter if the procedure returned every field defined in the entity.  It appears that in EF you can’t do this unless I’m missing something.  I keep getting the following exception:

    The data reader is incompatible with the specified 'MyEntityClass'. A member of the type, 'MyEntityProperty', does not have a corresponding column in the data reader with the same name.

    I hope I don’t have to define a bunch of complex types for all my procedures, what a pain.  If this is the case, why is this?



    Thursday, December 30, 2010 5:17 AM


  • For stored procedure Ef uses the same materialization mechanism as it does when it tries to populates entities from EF queries. Not sure if this requirement would be relaxed in future versions. You have few options.

    1. Add dummy columns to the the stored procedure query to complete all the columns it needs to materialize the result. 

    2. UseExecuteStoreQuery<MyCustomType>("exec myproc"). Any nominal class that is not bound to the model will not have this restriction.

    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    • Proposed as answer by Jackie-Sun Monday, January 3, 2011 2:16 AM
    • Marked as answer by Jackie-Sun Wednesday, January 5, 2011 2:40 AM
    Thursday, December 30, 2010 7:19 AM