38

I have a stored procedure that contains dynamic select. Something like this:

ALTER PROCEDURE [dbo].[usp_GetTestRecords] 
    --@p1 int = 0, 
    --@p2 int = 0
    @groupId nvarchar(10) = 0
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @query  NVARCHAR(max)

    SET @query = 'SELECT * FROM CUSTOMERS WHERE Id = ' + @groupId
    /* This actually contains a dynamic pivot select statement */

    EXECUTE(@query);
END

In SSMS the stored procedure runs fine and shows result set.

In C# using Entity Framework it shows returning an int instead of IEnumerable?

private void LoadTestRecords()
{
    TestRecordsDBEntities dataContext = new TestRecordsDBEntities();
    string id = ddlGroupId.SelectedValue;

    List<TestRecord> list = dataContext.usp_GetTestRecords(id); //This part doesn't work returns int
    GridView1.DataSource = list;
}

Generated function for usp_GetTestRecords

public virtual int usp_GetTestRecords(string groupId)
{
    var groupIdParameter = groupId != null ?
        new ObjectParameter("groupId", groupId) :
        new ObjectParameter("groupId", typeof(string));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("usp_GetTestRecords", groupIdParameter);
}
10

17 Answers 17

33

I get this when I have a stored procedure that includes an "exec" call into a temporary table, such as:

insert into #codes (Code, ActionCodes, Description)
exec TreatmentCodes_sps 0

It appears that Entity Framework gets confused as to what should be returned by the procedure. The solution I've come across is to add this at the top of the sproc:

SET FMTONLY OFF

After this, all is well.

32

I got the same problem, and found solution here

  1. Move to your .edmx
  2. At Model Browser Window/Function Imports find your procedure then double click it
  3. Change the return type to you want
  4. Save .edmx and check the return type again.

Screenshot

It should be what you need now.

2
  • 1
    This fixed my problem. Thank you. Oct 20, 2016 at 6:00
  • 1
    what a complete faff. I have to do this everytime my model updates. god help anyone working code first. this is costing me hours everyday
    – djack109
    Aug 1, 2017 at 22:34
17

Entity Framework can't tell what your stored procedure is returning. I've had success creating a table variable that mirrors the data from your SELECT statement. Just insert into the table variable then do a select from that table variable. EF should pick it up.

5
  • 1
    This is really the way to go. You can even do it just temporarily, but beware that you could be in a bind if one were to regenerate the EF model. EF doesn't pick up dynamic SQL so you need a temp table or table variable for it to read from to generate the complex type.
    – wilsjd
    Aug 15, 2013 at 18:56
  • 2
    Could you please explain this part a little clearer: "Just insert into the table variable then do a select from that table variable. EF should pick it up." ? I'm not sure what to do on that.
    – Termato
    Jul 14, 2014 at 18:42
  • This didn't work, it even caused problems for the actual solution below, on the model you can double click functions and set the return type. If you use temp tables that doesn't work.
    – John
    Mar 3, 2017 at 8:04
  • 1
    EF is a pain in the backside
    – djack109
    Aug 1, 2017 at 22:21
  • Here is a step by step solution of this issue which worked for me sandeepknarware.in/?p=247 Dec 30, 2017 at 12:30
11

See Ladislav Mrnka's answer in this Stack Overflow post https://stackoverflow.com/a/7131344/4318324

I had the same basic problem.

Adding

SET FMTONLY OFF

To a procedure you are trying to import during the import will address this problem. It's a good practice to remove the line afterwards unless the purpose of the database is solely to provide schema for EF (Entity Framework).

The main reason for caution is that EF uses this setting to prevent data mutations when trying to obtain metadata.

If you refresh your entity model from a database any procedures with this line in them can potentially update the data in that database just by trying to obtain the schema.

I wanted to add a further note on this so it's not needed to fully scan through the other link.

if you want to try to use FMTONLY here are a couple things to keep in mind.

when FMTONLY is on:
  1) only the schema is returned (no) rows.
     similar to adding a blanket false statement to your where clause (ie "where 1=0")
  2) flow control statements are ignored

Example

set fmtonly on

if 1=1
begin
    select 1 a
end
else
begin
    select 1 a,2 b
end

while 1=1
select 1 c

The above returns NO rows whatsoever and the metadata for each of the three queries

For this reason some people suggest toggling it off in a way that takes advantage of it's non-observance of flow control

if 1=0
begin
    set fmtonly off
end

In fact you could use this to introduce logic that tracks this

set fmtonly off
declare @g varchar(30)
set @g = 'fmtonly was set to off'

if 1=0
begin
    set fmtonly off
    set @g = 'fmtonly was set to on' 
end

select @g

Think VERY CAREFULLY before trying to use this feature as it is both deprecated and potentially makes sql extremely hard to follow

the MAIN concepts that need to be understood are the following

1. EF turns FMTONLY on to prevent MUTATING data from executing stored procedures
   when it executes them during a model update.
   (from which it follows)

2. setting FMTONLY off in any procedure that EF will attempt to do a schema scan
   (potentially ANY and EACHONE) introduces the potential to mutate database
   data whenever *anyone* attempts to update their database model.
1
7

Entity Framework will automatically return a scalar value if your stored procedure doesn't have a primary key in your result set. Thus, you'd have to include a primary key column in your select statement, or create a temp table with a primary key in order for Entity Framework to return a result set for your stored procedure.

2

I had the same problem, I changed the name of return fields by 'AS' keyword and addressed my problem. One reason for this problem is naming column names with SQL Server reserved keywords.

The example is fallows:

ALTER PROCEDURE [dbo].[usp_GetProducts]

AS
BEGIN

 SET NOCOUNT ON;

 SELECT 
      , p.Id
      , p.Title
      , p.Description AS 'Description'

    FROM dbo.Products AS p
END
2

Best solution I found is to cheat a little.

In the store procedure, comment everything, put a first line with a select [foo]='', [bar]='' etc... Now update the model, go to the mapped function, select complex type and click on Get Column Information and then Create Complex Type.

Now comment the fake select and un-comment the real store procedure body.

1

When you generated your model class for your stored procedure, you chose scalar return result by mistake. you should remove your stored procedure from your entity model, then re-add the stored procedure. In the dialog for the stored procedure, you can choose the return type you are expecting. Do not just edit the generated code.. this may work now, but the generated code can be replaced if you make other changes to your model.

2
  • 2
    I generated the Model from the Database. It automatically gave it the int as a return type
    – Rod
    Aug 15, 2013 at 15:29
  • 2
    Even if he chose a complex return type, EF wouldn't pick it up because EF doesn't recognize dynamic SQL.
    – wilsjd
    Aug 15, 2013 at 18:58
1

I have pondered this a bit and I think I have a better/simpler answer

If you have a complex stored that gives entity framework some difficultly (for current versions of Entity Framework that are using the FMTONLY tag to aquire schema)

consider doing the folowing at the beginning of your stored procedure.

--where [columnlist] matches the schema you want EF to pick up for your stored procedure

if 1=0
begin
    select 
       [columnlist]
    from [table list and joins]
    where 1=0
end

if you are okay loading your result set into a table variable you can do the following to help keep your schema in sync

declare @tablevar as table
(
    blah int
    ,moreblah varchar(20)
)

if 1=0
begin
    select * from @tablevar
end

...
-- load data into @tablevar
select * from @tablevar
3
  • the thing to look out for here is that if you change the schema your procedure returns, you will need to update the beginning piece to get EF to pick up those changes.
    – wode
    Aug 14, 2017 at 20:04
  • this works since EF ignores flow control logic in it's search for the first returned dataset schema to build it's model with.
    – wode
    Aug 14, 2017 at 20:05
  • added an option to address schema updates. note: "go" 's will cause problems for table variables and the table variable will work where a regular temp table will not (at least for my version SQL Server 2014 12.0.5000.0 anyone have confirmation for other versions?)
    – wode
    Aug 14, 2017 at 20:22
1

If you need to do this, then you might be better off just making a partial of the dbcontext and creating the C# function yourself that will use SqlQuery to return the data you need. Advantages over some of the other options is:

  1. Don't have to change anything when the model updates
  2. Won't get overwritten if you do it directly in the generated class (someone above mention this as if it's an option :) )
  3. Don't have to add anything to the proc itself that could have side effects now or later on

Example Code:

 public partial class myEntities
    {
       public List<MyClass> usp_GetTestRecords(int _p1, int _p2, string _groupId)
       {
          // fill out params
          SqlParameter p1 = new SqlParameter("@p1", _p1);
          ...
          obj[] parameters = new object[] { p1, p2, groupId };

          // call the proc
          return this.Database.SqlQuery<MyClass>(@"EXECUTE usp_GetTestRecords @p1, @p2, @groupId", parameters).ToList();
       }
    }
0

Just change to

ALTER PROCEDURE [dbo].[usp_GetTestRecords] 
    --@p1 int = 0, 
    --@p2 int = 0
    @groupId nvarchar(10) = 0
AS
BEGIN
    SET NOCOUNT ON;


    SELECT * FROM CUSTOMERS WHERE Id =  @groupId

END
0

I know this is an old thread but in case someone has the same problems I'll tell my woes.

As a help to find the issue, run sql profiler when you add your stored proc. Then you can see what entity framework is passing as parameters to generate your resultset. I imagine nearly always it will pass null parameter values. If you are generating sql on the fly by concatenating string values and parameter values and some are null then the sql will break and you wont get a return set.

I haven't needed to generate temp tables or anything just an exec command.

Hope it helps

0

During import

SET FMTONLY ON can be used for taking the sp schema.

If you change the sp and want to update the new one, you should delete the old defined function from edmx file (from xml), because although deleting sp from model browser, it is not deleted in edmx. For example;

    <FunctionImport Name="GetInvoiceByNumber"     ReturnType="Collection(Model.Invoice_Result)">
       <Parameter Name="InvoiceNumber" Mode="In" Type="Int32" />
    </FunctionImport>

I had the same problem, and when I delete the FuctionImport tag of corresponding sp totally, the model updated right. You can find the tag by searching the function name from visual studio.

0

You may have luck opening up the model browser, then going to Function Imports, double clicking the stored procedure in question and then manually clicking "Get Column Information" and then clicking "Create New Complex Type". This usually sorts out the problem.

0

Well I had this issue as well but after hours of online searching none of above methods helped. Finally I got to know that It will happen if your store procedure is getting some parameters as null and which generate any error in query execution. Entity Framework will generate method for store procedure by defining the complex entity model. Due to that null value your store procedure will return and int value.

Please check your store procedure either its providing empty result set with null values. It will fix your problem. Hopefully.

0

I think this is a problem of permissions on the database, I don't know what exactly could be, but, in my job we use Active Directory users to grant applications connect to databases, this accounts are specially created for the applications, each app has its own user account, well, as a developers I have permissions for read, write and other basic things, no alter, and no advanced features, and I have this same problem running Visual Studio with my normal account, then, what I did was to open Visual Studio selecting the option "as a different user" on the context menu, and I put the AD login granted for the application and voila!, now my Stored Procedures are loading with all the fields I was expected, before that, my Stored Procedures was returning as int. I hope this help someone, maybe the VIEW DEFINITION permissions on database account do the trick

0

If SQL Authentication is in place, verify that the user credential that is being used to connect Entity Framework to the database has the proper rights to read from CUSTOMERS table.

When Entity Framework uses SQL Authentication to map complex objects (i.e stored procedures that SELECTs more than one column), if any of the tables from within such stored procedure don't have set up the Read permission, the mapping will result in returning INT instead of the desired Result set.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.