Solved

Entity framework and stored procedures

Posted on 2014-09-25
6
937 Views
Last Modified: 2014-09-27
Hi.
why two different procedures but similar in their content, both with select * from table return different results for the entity framework 6.1 from database? Both have result (0) at the end. one returns an integer and the other returns a ObjectResult.

thx in advance

this is the working stored procedure:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_Pessoas]
@PessoaID            integer=0,
@CDTipoPessoa        char(1)='',
@PessoaEmpresaID     integer=0,
@NomePessoa          varchar(100)='',
@Fantasia            varchar(50)='',
@CodigoExterno       integer=0            
as

if  @CodigoExterno  is  not  null
    if  @CodigoExterno>0  and  @PessoaID=0
          set  @PessoaID=DBASE.dbo.PessoasConverteCodigoExterno(@CDTipoPessoa,@CodigoExterno)

select *
  from DBASE.dbo.Pessoas (nolock)
 where (@PessoaID=0         or  PessoaID=@PessoaID)                   and
       (@CDTipoPessoa=''    or  CDTipoPessoa=@CDTipoPessoa)           and
       (@PessoaEmpresaID=0  or  PessoaEmpresaID=@PessoaEmpresaID)     and
       (@NomePessoa=''      or  NomePessoa like '%'+@NomePessoa+'%')  and
       (@Fantasia=''        or  Fantasia   like '%'+@Fantasia+'%')        
 order by NomePessoa
 
return(0)

and this is the result as expected:
var resultado = DataContextFabrica.GetDataContext().sp_Pessoas(pessoaID, "", 0, "", "", codigoExterno).FirstOrDefault();


this is then, the not working sp:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_MYUSERS]
@Opcao                 char(1)='',
@PessoaID              integer=0,
@Login                 varchar(50)='',
@Senha                 varchar(50)='',
@CodigoExterno         integer=0

as

if  @CodigoExterno  is  not  null
    if  @CodigoExterno>0  and  @PessoaID=0
          set  @PessoaID=DBASE.dbo.PessoasConverteCodigoExterno('U',@CodigoExterno)
 
select *
      
  from DBASE.dbo.PessoasLogins   a (nolock),
       DBASE.dbo.Pessoas  b (nolock)
 where a.PessoaID=b.PessoaID                and
       a.PessoaEmpresaID=b.PessoaEmpresaID  and
       b.StatusPessoa<>'9'                  and
     ((@opcao='P'  and  a.PessoaID=@PessoaID)  or
      (@opcao='L'  and  a.Login=@Login  and  a.Senha=@Senha))

return(0)

this is the result that I expected:
ObjectResult<sp_MYUSERS_Result> resultado = DataContextFabrica.GetDataContext().sp_MYUSERS(opcao, pessoaID, login, senha, codigoExterno);

and this is what I got:
Error      21      Cannot implicitly convert type 'int' to 'System.Data.Entity.Core.Objects.ObjectResult<sp_MYUSERS_Result>'      C:\Users\XXXXServicos\Main\Servicos.Dados\Repositorios\PessoasLoginRepositorio.cs      86      72      Services.Dados
0
Comment
Question by:Deraldo Silva
  • 3
  • 2
6 Comments
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40345499
A Stored Procedure doesn't return values. Function does.

What I can see from your code is that's a little difference. In the first example it returns the value from the FirstOrDefault() function. The second example doesn't has a function.
0
 

Author Comment

by:Deraldo Silva
ID: 40345688
HI Vitor, thx for your attention.
but, I have others stored procs without function and they works.
The function here is to return a single object while the other is for a list.

you see this code in the second SP?
if  @CodigoExterno  is  not  null
     if  @CodigoExterno>0  and  @PessoaID=0
           set  @PessoaID=DBASE.dbo.PessoasConverteCodigoExterno('U',@CodigoExterno)

if I remove the 'U' and change it to a proc parameter as in the first proc, it works. however, I dont know why!
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 40345690
I need to see the PessoasConverteCodigoExterno code. Maybe that parameter it's an output parameter and if so can only accept variables and not values.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:Deraldo Silva
ID: 40346706
Ok. that is.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[PessoasConverteCodigoExterno](
@CDTipoPessoa     int,
@CodigoExterno    int)
returns integer
as
begin

declare
@wCodigoInterno  integer

set  @wCodigoInterno=(
     select top 1 a.CodigoInterno
       from DBASE.dbo.PessoasMigracaoCorrelacoes a (nolock),
              DBASE.dbo.Pessoas                    b (nolock)
      where a.CodigoInterno=b.PessoaID        and
            a.CDTipoPessoa=b.CDTipoPessoa     and
                  a.CDTipoPessoa=@CDTipoPessoa      and
                  a.CodigoExterno=@CodigoExterno)

if  @wCodigoInterno  is  null
    set  @wCodigoInterno=0

return @wCodigoInterno

end
0
 
LVL 46

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40347480
This is a function and not SP, that's why returns a value.
The problem is that you are passing 'U' that it's char but the function is expecting an integer "@CDTipoPessoa     int,"
In first example it works because the variable it's '' so SQL Server engine converts it to 0 (zero).
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 40347500
This is more precisely what Victor is refering to:

 set  @PessoaID=DBASE.dbo.PessoasConverteCodigoExterno('U',@CodigoExterno)

first parameter is a of varchar(string) type, 'U', but it should be an integer as per function definition. If it is actually a varchar then you need to change the function definition like this:

ALTER function [dbo].[PessoasConverteCodigoExterno](
@CDTipoPessoa     varchar(10),
@CodigoExterno    int)

I used 10 but it should be of the same size as the column you compare it to, a.CDTipoPessoa in this case. Remember, this will work if the a.CDTipoPessoa column is a varchar as well. If not than keep it as is but make sure you pass an integer instead of a varchar.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help debbuging stored procedure 21 35
Syntax error 9 45
How do I prevent the pages in my site from showing up in a google search? 2 43
Sql Query Datatype 2 20
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now