Deraldo Silva
asked on
Entity framework and stored procedures
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.Pessoa sConverteC odigoExter no(@CDTipo Pessoa,@Co digoExtern o)
select *
from DBASE.dbo.Pessoas (nolock)
where (@PessoaID=0 or PessoaID=@PessoaID) and
(@CDTipoPessoa='' or CDTipoPessoa=@CDTipoPessoa ) and
(@PessoaEmpresaID=0 or PessoaEmpresaID=@PessoaEmp resaID) 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.GetData Context(). sp_Pessoas (pessoaID, "", 0, "", "", codigoExterno).FirstOrDefa ult();
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.Pessoa sConverteC odigoExter no('U',@Co digoExtern o)
select *
from DBASE.dbo.PessoasLogins a (nolock),
DBASE.dbo.Pessoas b (nolock)
where a.PessoaID=b.PessoaID and
a.PessoaEmpresaID=b.Pessoa EmpresaID 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_Re sult> resultado = DataContextFabrica.GetData Context(). 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.O bjects.Obj ectResult< sp_MYUSERS _Result>' C:\Users\XXXXServicos\Main \Servicos. Dados\Repo sitorios\P essoasLogi nRepositor io.cs 86 72 Services.Dados
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.Pessoa
select *
from DBASE.dbo.Pessoas (nolock)
where (@PessoaID=0 or PessoaID=@PessoaID) and
(@CDTipoPessoa='' or CDTipoPessoa=@CDTipoPessoa
(@PessoaEmpresaID=0 or PessoaEmpresaID=@PessoaEmp
(@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.GetData
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.Pessoa
select *
from DBASE.dbo.PessoasLogins a (nolock),
DBASE.dbo.Pessoas b (nolock)
where a.PessoaID=b.PessoaID and
a.PessoaEmpresaID=b.Pessoa
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_Re
and this is what I got:
Error 21 Cannot implicitly convert type 'int' to 'System.Data.Entity.Core.O
ASKER
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.Pessoa sConverteC odigoExter no('U',@Co digoExtern o)
if I remove the 'U' and change it to a proc parameter as in the first proc, it works. however, I dont know why!
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.Pessoa
if I remove the 'U' and change it to a proc parameter as in the first proc, it works. however, I dont know why!
I need to see the PessoasConverteCodigoExter no code. Maybe that parameter it's an output parameter and if so can only accept variables and not values.
ASKER
Ok. that is.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[PessoasConverteCodi goExterno] (
@CDTipoPessoa int,
@CodigoExterno int)
returns integer
as
begin
declare
@wCodigoInterno integer
set @wCodigoInterno=(
select top 1 a.CodigoInterno
from DBASE.dbo.PessoasMigracaoC orrelacoes a (nolock),
DBASE.dbo.Pessoas b (nolock)
where a.CodigoInterno=b.PessoaID and
a.CDTipoPessoa=b.CDTipoPes soa and
a.CDTipoPessoa=@CDTipoPess oa and
a.CodigoExterno=@CodigoExt erno)
if @wCodigoInterno is null
set @wCodigoInterno=0
return @wCodigoInterno
end
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[PessoasConverteCodi
@CDTipoPessoa int,
@CodigoExterno int)
returns integer
as
begin
declare
@wCodigoInterno integer
set @wCodigoInterno=(
select top 1 a.CodigoInterno
from DBASE.dbo.PessoasMigracaoC
DBASE.dbo.Pessoas b (nolock)
where a.CodigoInterno=b.PessoaID
a.CDTipoPessoa=b.CDTipoPes
a.CDTipoPessoa=@CDTipoPess
a.CodigoExterno=@CodigoExt
if @wCodigoInterno is null
set @wCodigoInterno=0
return @wCodigoInterno
end
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is more precisely what Victor is refering to:
set @PessoaID=DBASE.dbo.Pessoa sConverteC odigoExter no('U',@Co digoExtern o)
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].[PessoasConverteCodi goExterno] (
@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.
set @PessoaID=DBASE.dbo.Pessoa
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].[PessoasConverteCodi
@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.
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.