Use stored procedure in EF(MVC4) - entity-framework

I have a storedprocedure "SelectKP" (Datetime bt,Datetime et), please help to create a class and a repository for this procedure.

Look at this link:
How to execute a stored procedure within C# program
There is not a function generated manually, but it executes stored procedure

Here is simple example.
using (var db = new ModelEntities())
{
var query = db.ExecuteStoreQuery<ModelClass>("StoreProcedureName '" + UserId +"'");
}

Related

Linq2db.EntityFrameworkCore against Oracle

I don't understand why linq2db is not generating the correct SQL string for this query against Oracle, and it is working perfectly against SQLServer.
string idUser="U1";
await context.Users.ToLinqToDbTable().Where(u=>u.IdUser == idUser).FirstOrDefaultAsyncLinqToDB();
Always returns null. However, it works if el value used directly:
await context.Users.ToLinqToDbTable().Where(u=>u.IdUser == "U1").FirstOrDefaultAsyncLinqToDB();
Looking at the logs, el SQL statement generated is like this:
-- Oracle Oracle11
DECLARE #idUser Varchar2(8) -- String
SET #idUser = 'U1'
SELECT
u.IdUser,
etc..
FROM
USERS u
WHERE
u.IDUSER = :idUser
Why is it generating T-SQL code and not Oracle code (the context is initialized with .UseOracle())?
Thanks

How do I call a stored procedure from NHibernate that has a result using C#?

I have a stored procedure that loads data and returns on record in a string format. How do I call it and capture the result using NHibernate
Eg below
var query = session.CreateSQLQuery("EXECUTE LoadData #ActualsDate =:ActualsDate, ActualBroadcastMonth =:ActualsMonth")
.SetTimeout(0)
.SetParameter("ActualsDate", actualsDate)
.SetParameter("ActualsMonth", actualsMonth)
.???;
return ...;```
Got the answer
.SetTimeout(0)
.SetParameter("ActualsDate", actualsDate)
.SetParameter("ActualsMonth", actualsMonth)
.UniqueResult;
return query.ToString();

Entity Framework Core, Stored Procedure

I am totally confused regarding how to use Stored Procedures using Entity Framework Core. If the stored procedure return an anonymous type, how do I retrieve the data? If the return type is not anonymous, what should I do? How do I add input/output parameters?
I am asking these questions because everywhere I look, I get a different answer. I guess EF Core is evolving rapidly and Microsoft is dabbling with a lot of ideas.
How do I add input/output parameters?
I'm going to answer this particular question of yours.
Below is a TSQL stored procedure with two input and two output parameters
CREATE PROCEDURE [dbo].[yourstoredprocedure]
-- Add the parameters for the stored procedure here
#varone bigint
,#vartwo Date
,#varthree double precision OUTPUT
,#varfour bigint OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- YOUR CODE HERE
SET #varthree = 10.02;
SET #varfour = #varone;
return;
END
Now To execute this stored procedure using Entity Framework Core
MyContext.Database
.ExecuteSqlCommand(#"EXECUTE [yourstoredprocedure] " +
" {0} " +
", {1} " +
",#varthree OUTPUT " +
", #varfour OUTPUT ", dataOne, dataTwo, outputVarOne, outputVarTwo);
var outputResultOne= outputVarOne.Value as double?;
var outputResultTwo= outputVarTwo.Value as long?;
You can pass your input simply using parameterized query as above. You can also create named parameters. such as for output parameters, I've created two named parameters as -
var outputVarOne = new SqlParameter
{
ParameterName = "#varthree ",
DbType = System.Data.DbType.Double,
Direction = System.Data.ParameterDirection.Output
};
var outputVarTwo = new SqlParameter
{
ParameterName = "#varfour ",
DbType = System.Data.DbType.Int64,
Direction = System.Data.ParameterDirection.Output
};
And This is how using EF Core you execute a stored procedure with input and output parameters. Hope this helps someone.
This solution provides methods that call a stored procedure and maps the returned value to a defined (non-model) entity. https://github.com/verdie-g/StoredProcedureDotNetCore
Microsoft address this issue:
"SQL queries can only be used to return entity types that are part of your model. There is an enhancement on our backlog to enable returning ad-hoc types from raw SQL queries." https://learn.microsoft.com/en-us/ef/core/querying/raw-sql
And here is the issue tracked in GitHub: https://github.com/aspnet/EntityFramework/issues/1862
you might use an extention like StoredProcedureEFCore
Then the usage is more intuitively.
List rows = null;
ctx.LoadStoredProc("dbo.ListAll")
.AddParam("limit", 300L)
.AddParam("limitOut", out IOutParam<long> limitOut)
.Exec(r => rows = r.ToList<Model>());
long limitOutValue = limitOut.Value;
ctx.LoadStoredProc("dbo.ReturnBoolean")
.AddParam("boolean_to_return", true)
.ReturnValue(out IOutParam<bool> retParam)
.ExecNonQuery();
bool b = retParam.Value;
ctx.LoadStoredProc("dbo.ListAll")
.AddParam("limit", 1L)
.ExecScalar(out long l);

SqlCommand (CommandType Text) retrieve param collection

Is there a way to get the parameter collection from SqlCommand when CommandType = Text?
For instance:
string MyDinamicSql = #"SELECT * FROM USERS where USERName = #Param1 and USERLogin=#Param2";
SqlCommand comand = connection.CreateCommand();
comand.CommandText = MyDinamicSql;
comand.CommandType = System.Data.CommandType.Text;
//Do something for fill comand.Parameters from MyDinamicSql...
I want a way to get a parameter collection with [#Param1,#Param2]... I don't know the sql queries (they are dynamic) and I want to get the parameters for my application to create them as input controls.
I can't do this with SqlCommandBuilder.DeriveParameters(command), because it only works with stored procedures.
Thank you!
No - until SQL Server parses the command, it's just a string.

Can I use SQLParameter when having a variable quantity of fields to update?

I have a table with eighty fields, none to seventy of them can change depending of an update process I have. For example:
if (process.result == 1)
cmd.CommandText = "UPDATE T SET f1=1, f6='S'" ;
else if (Process.result == 2)
cmd.CommandText = string.Format("UPDATE T SET f1=2, f12={0},f70='{1}'", getData(), st);
else if ..... etc.
I can optimize the building process of the UPDATE statement, however I would like to use SQLParameter; is that possible and convenient given the variablity of data to update?
Thanks.
For each if statement you currently are using inline string formats, you could just as well just add the sql params instead.
The formatting of the UPDATE string could be replaced with the selected sql params you require to be updated insted.