EF 6 database first: How to update stored procedures?

让人想犯罪 __ 提交于 2019-11-29 22:49:40
Rick V

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.
andersr

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!