47

We are using Entity Framework 6.0.0 and use database first (like this) to generate code from tables and stored procedures. This seems to work great, except that changes in stored procedures are not reflected when updating or refreshing the model. Adding a column to a table is reflected, but not adding a field to a stored procedure.

It is interesting that if I go to the Model Browser, right click the stored procedure, select Add Function Import and click the button Get Column Information we can see the correct columns. This means that the model knows of the columns, but does not manage to update the generated code.

There is one workaround, and that is to delete the generated stored procedure before updating the model. This works as long as you have not made any edits on the stored procedure. Does anyone know of a way to avoid this workaround?

I am using Visual Studio 2013 with all the latest updates as of early December 2013.

Thanks in advance!

Update 1: andersr's answer helped in one case, where the stored procedure used a temporary table, so i gave him +1, but it still does not solve the main problem of updating simple stored procedures.

Update 2: shimron's comment below links to a question about the same issues in EF 3.5. It seems the same is still true for EF 6.0. Read it for an alternative way of doing it, but my conclusion as of now is that the simplest way of doing it is to delete the generated stored procedure before updating the model. Use partial classes if you want to do something fancy.

1

3 Answers 3

104

Based on this answer by DaveD, these steps address the issue:

  1. In your .edmx, rt-click and select Model Browser.
  2. Within the Model Browser (in VS 2015 default configuration, it is a tab within the Solution Explorer), expand Function Imports under the model.
  3. Double-click your stored procedure.
  4. Click the Update button next to Returns a Collection Of - Complex (if not returning a scalar or entity)
  5. Click okay then save your .edmx to reflect field changes to your stored procedure throughout your project.
4
  • 5
    This updates the complex type generated for the return value. Any idea how to update the parameters of the stored procedure in you model after updating the database?
    – markmnl
    Jun 1, 2017 at 6:57
  • 3
    To answer my own question refreshing the entire model works using the Update Model from Database... wizard.
    – markmnl
    Jun 1, 2017 at 6:59
  • What if you don't have a .edmx file? May 11, 2018 at 13:07
  • Just an FYI this approach, while valid in all versions of Visual Studio 2017, this approach does not work in Visual Studio 2019 version 16.0.0 Apr 8, 2019 at 18:11
3

Does your stored procedures return data from temporary tables by any chance ? EF does not seem to support this, see EF4 - The selected stored procedure returns no columns for more information.

However, the stored procedure will as you observed, be available in the Model Browser. I did a quick test featuring the scenario described above. The stored procedure was generated in my context class, but the return type was an int rather than a complex type. See the link above for potential workarounds.

1
  • +1 because this helped with a stored procedure using a temporary table, but it did not really solve the main question (see my update above). Thanks anyway!
    – Halvard
    Dec 12, 2013 at 10:55
0

I just encountered this and my workaround (it is really nasty) was to create an if statement with a condition that will never be true at the top of the stored procedure which selects the same list of outputs as the query with explicit casting to the datatypes I want to return. This will assume nullability of your types, so to resolve that you wrap the cast in an ISNULL

For example, if your output has the columns:

UserId (int, not null)
RoleId (int, nullable)
FirstName (varchar(255), nullable)
Created (datetime, not null)

You would expect this to create a POCO like:

SomeClass {
    public int UserId { get; set; }
    public int? RoleId { get; set; }
    public string FirstName { get; set; }
    public DateTime Created { get; set; }
}

...But it doesn't and that's why we're here today. To get around this not working as expected, I put the following at the top of my SP (right after the 'AS'):

if(1=0)
begin
    select
        UserId = isnull((cast(0 as int)),0),
        RoleId = cast(0 as int),
        FirstName = cast(0 as varchar),
        DateTime = isnull((cast(0 as datetime)),'')
end

It is horrible and ugly but it works for me every time. Hopefully we get a tooling update that resolves this soon...happened to me today with no temp tables in SQL Server 2016 w/VS2015...

Hope this helps somebody

4
  • Isn't it better to delete and then re-add the SP?
    – Andrew
    Mar 29, 2017 at 1:19
  • Yes, worlds better - but unfortunately deleting and re-adding the SP gave me the same bizarre result Mar 29, 2017 at 14:41
  • 1
    I think if you open the Model Browser, delete the SP, the complex type and the function import, close and reopen the solution and add the SP, it has to work!
    – Andrew
    Mar 29, 2017 at 18:42
  • 1
    Yes you need to delete all 3 artifacts: sp, complex type and function import for it to work
    – Jeremy
    Jul 27, 2017 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.