How does EF determine nullable parameters? - c#

I have a SQL stored procedure and I need to be able to pass a NULL as a value to one of its parameters to be used in a query like this:
create procedure sp
#param1 varchar(30)
as
select * from table where field = isnull(#param1, field)
So I need to some how tell EF to make #param1 nullable. How do I do this?
Thanks!
In case it helps, the process I use with EF is:
Create SP
Update Model (edmx)
Add new function import
Generate new complex type by clicking the button
Run Custom Tool on separate template file (to generate POCO)

As a work-around, you could declare two separate stored procedures:
-- use this for non-null parameters
create procedure sp
#param1 varchar(30)
as
select * from table where field = #param1
-- use this for null
create procedure sp_null
as
select * from table
and then you can write the desired abstraction in C#:
public ... GetSp(string param1)
{
if (param1 == null)
return ....sp_null();
else
return ....sp(param1);
}

Quick look and I found this on stackoverflow. Hope it helps.
Entity Framework 4.0 Entity SQL passing null ObjectParameter parameters

Use DBNull.Value, I've done exactly this with one of my stored procedures. To call your procedure my code would look like:
List<ObjectParameter> objectParameterList = new List<ObjectParameter>();
if (string.IsNullOrEmpty(param1))
{
object nullValue = DBNull.Value;
objectParameterList.Add(new ObjectParameter("param1", nullValue));
}
else
{
objectParameterList.Add(new ObjectParameter("param1", param1));
}
context.ExecuteFunction("MyEFModel.sp", objectParameterList.ToArray());
Hopefully this helps.

set the default value in Stored procedure as NULL.
create procedure sp
#param1 varchar(30) =NULL
as
select * from table where field = isnull(#param1, field)

Related

Return stored procedure data as table set

I am using .net with entity framework database first approeach. I was wondering if it's possible for stored procedure to return result as table set instead of procedure result set.
Suppose I have a table:
table MyTable(
id int,
value nvarchar(max))
And stored procedure:
procedure GetMyTableSet
AS
Select *
From MyTable A
Where A.Value = 'test'
Now the type that the procedure returns in .net will be GetMyTableSet_Result, but is it possible for it to be MyTable because it has the same data structure? Can I cast it in procedure somehow or do something else?
EDIT: In .net my procedure is generated like this:
public virtual ObjectResult<GetMyTableSet_Result> GetMyTableSet(string accessRight, Nullable<int> companyId, string accessParam, Nullable<bool> multipleCompanies)
{
var accessRightParameter = accessRight != null ?
new ObjectParameter("accessRight", accessRight) :
new ObjectParameter("accessRight", typeof(string));
var companyIdParameter = companyId.HasValue ?
new ObjectParameter("companyId", companyId) :
new ObjectParameter("companyId", typeof(int));
var accessParamParameter = accessParam != null ?
new ObjectParameter("accessParam", accessParam) :
new ObjectParameter("accessParam", typeof(string));
var multipleCompaniesParameter = multipleCompanies.HasValue ?
new ObjectParameter("multipleCompanies", multipleCompanies) :
new ObjectParameter("multipleCompanies", typeof(bool));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<GetMyTableSet_Result>("GetMyTableSet", accessRightParameter, companyIdParameter, accessParamParameter, multipleCompaniesParameter);
}
What I wanted to know if it's possible for it to be like ObjectResult<MyTable> instead of ObjectResult<GetMyTableSet_Result> as the procedure returns the same data structure as it is for MyTable table.
You are returning an entity which can be stored in an object having any name you like. You could also use a VIEW rather than a stored procedure.
In code terms and using EF contexts:
MyEntity dbEntity = new MyEntity();
var MyTable = dbEntity.GetMyTableSet;
EF will do the mapping of data types so the returned object will mirror the types returned by the stored procedure (unless you use CAST or CONVERT to change the column type.
You can take a look here to understand how to call procedures from Entity Framework. EF will do all the mappings between result set column values and properties based on column name, but you must take care of data type compatibility.
Also, I think you can control the mappings by specifying that some properties should not be mapped.

Pass table value type to SQL Server stored procedure via Entity Framework

I created a user-defined table type in SQL Server:
CREATE TYPE dbo.TestType AS TABLE
(
ColumnA int,
ColumnB nvarchar(500)
)
And I'm using a stored procedure to insert records into the database:
create procedure [dbo].[sp_Test_CustomType]
#testing TestType READONLY
as
insert into [dbo].[myTable]
select ColumnA, ColumnB
from #testing
And I would like to use EF to execute this stored procedure, but here's the problem: how can I pass a user defined table to the stored procedure?
I tried adding the stored procedure to the model, but I'm unable to find the desired stored procedure in the updated context.
What I'm trying to do is to execute a bulk insert to a table, here's the method that I'm currently using:
List<items> itemToInsertToDB = //fetchItems;
foreach(items i in itemToInsertToDB)
{
context.sp_InsertToTable(i.ColumnA, i.ColumnB)
}
Currently, I use a foreach loop to loop through the list to insert item to DB, but if the list have a lot of items, then there will be a performance issue, so, I'm thinking of passing a list to the stored procedure and do the insert inside.
So how to solve this problem? or are there any better ways to do this?
Lets say you want to send a table with a single column of GUIDs.
First we need to create a structure using SqlMetaData which represents the schema of the table (columns).
The below code demonstrates one column named "Id" of the GUID is the SQL stored procedure parameter table type
var tableSchema = new List<SqlMetaData>(1)
{
new SqlMetaData("Id", SqlDbType.UniqueIdentifier)
}.ToArray();
Next you create a list of records that match the schema using SqlDataRecord.
The below code demonstrates how to add the items inside a list using the above created schema. Create a new SqlDataRecord for each of the items in the list. Replace SetGuid with the corresponding type and Replace Guid.NewGuid() as the corresponding value.
Repeat new SqlDataRecord for each item and add them to a List
var tableRow = new SqlDataRecord(tableSchema);
tableRow.SetGuid(0, Guid.NewGuid());
var table = new List<SqlDataRecord>(1)
{
tableRow
};
Then create the SqlParameter:
var parameter = new SqlParameter();
parameter.SqlDbType = SqlDbType.Structured;
parameter.ParameterName = "#UserIds"; //#UserIds is the stored procedure parameter name
parameter.TypeName = "{Your stored procedure type name}"
parameter.Value = table;
var parameters = new SqlParameter[1]
{
parameter
};
Then simply call the stored procedure by using the Database.SqlQuery.
IEnumerable<ReturnType> result;
using (var myContext = new DbContext())
{
result = myContext.Database.SqlQuery<User>("GetUsers #UserIds", parameters)
.ToList(); // calls the stored procedure
// ToListAsync(); // Async
{
In SQL Server, create your User-Defined Table Type (I suffix them with TTV, Table Typed Value):
CREATE TYPE [dbo].[UniqueidentifiersTTV] AS TABLE(
[Id] [uniqueidentifier] NOT NULL
)
GO
Then specify the type as a parameter (don't forget, Table Type Values have to be readonly!):
CREATE PROCEDURE [dbo].[GetUsers] (
#UserIds [UniqueidentifiersTTV] READONLY
) AS
BEGIN
SET NOCOUNT ON
SELECT u.* -- Just an example :P
FROM [dbo].[Users] u
INNER JOIN #UserIds ids On u.Id = ids.Id
END
I suggest you not using Stored Procedure to insert bulk data, but just rely to Entity Framework insert mechanism.
List<items> itemToInsertToDB = //fetchItems;
foreach(items i in itemToInsertToDB)
{
TestType t = new TestType() { ColumnA = i.ColumnA, ColumnB = i.ColumnB };
context.TestTypes.Add(t);
}
context.SaveChanges();
Entity framework will smartly perform those insertion in single transaction and (usually) in single query execution, which will almost equal to executing stored procedure. This is better rather than relying on stored procedure just to insert bulk of data.

EF6 stored procedure must declare scalar variable

I am trying to call a stored procedure using C# EF6 to bring back data. I have tried to run the stored procedure in SQL management studio and it seems to work fine, however when I try to run it in my application I get an error saying "Must declare the scalar variable "#devID"
Here is part of my method in my application calling the stored procedure
public IHttpActionResult GetMetrics(int deviceID, string attribute, string startDate)
{
if (deviceID == 0)
{
return NotFound();
}
var metrics = db.Database.SqlQuery<Metrics>("GetMetrics #devID, #MetricType, #startTime", deviceID, attribute, startDate).ToList();
and here is my stored procedure:
ALTER PROCEDURE [dbo].[GetMetrics]
-- Add the parameters for the stored procedure here
#devID int,
#MetricType nvarchar(20),
#startTime nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT *
FROM dbMetrics
WHERE deviceID = #devID and MetricType = #MetricType and timeStamp >= #startTime
ORDER BY timeStamp
END
As per the documentation, if you want to use named parameters, you need to pass SqlParameter objects like this:
var metrics = db.Database.SqlQuery<Metrics>("GetMetrics #devID, #MetricType, #startTime",
new SqlParameter("devID", deviceID),
new SqlParameter("MetricType", attribute),
new SqlParameter("startTime", startDate)
).ToList();

Update table with stored procedure results in error

I have a SQL stored procedure for updating my table. But when executing the query via C#, xslt the one of the columns deleted from the table.
My stored procedure is
ALTER PROCEDURE [dbo].[kt_editingnotes]
(#NOTE NVARCHAR (512),
#ITEMNUMBER NVARCHAR (512),
#ACCOUNT NVARCHAR (512),
#CODE NVARCHAR(512)
)
AS
UPDATE NOTES
SET TXT = #NOTE
WHERE NOTESRECID = (SELECT ISP_EFAVORITLINE.ROWNUMBER
FROM ISP_EFAVORITLINE
WHERE ISP_EFAVORITLINE.ACCOUNT = #ACCOUNT
AND ISP_EFAVORITLINE.ITEMNUMBER = #ITEMNUMBER
AND ISP_EFAVORITLINE.CODE = #CODE)
return
and I am calling it like this:
ExecStoredProcedure('kt_editingnotes', concat('#ITEMNUMBER:', $ITEMNUMBER,', #ACCOUNT:', $ACCOUNT,', #CODE:', $CODE))
What is the problem? Can anyone help?
editingI noticed in the ExecStoredProcedure that you execute the procedure 'kt_deletenotes' instead of 'kt_editingnotes'. Try changing the line where you call your procedure to call the correct procedure.
ExecStoredProcedure('kt_editingnotes', concat('#ITEMNUMBER:', $ITEMNUMBER,', #ACCOUNT:', $ACCOUNT,', #CODE:', $CODE))

Linq to SQL, Stored Procedure with different return types based on an If/Else

I have an existing Stored Procedure which I am trying to now call with LINQ to SQL, here is the stored procedure:
ALTER procedure [dbo].[sp_SELECT_Security_ALL] (
#UID Varchar(15)
)
as
DECLARE #A_ID int
If ISNULL(#UID,'') = ''
SELECT DISTINCT
App_ID,
App_Name,
App_Description,
DB,
DBNameApp_ID,
For_One_EVA_List_Ind
From v_Security_ALL
ELSE
BEGIN
Select #A_ID = (Select Assignee_ID From NEO.dbo.v_Assignees Where USER_ID = #UID and Inactive_Ind = 0)
SELECT DISTINCT
Security_User_ID,
Security_Company,
Security_MailCode,
Security_Last_Name,
Security_First_Name,
Security_User_Name,
Security_User_Info,
Security_User_CO_MC,
Security_Email_Addr,
Security_Phone,
Security_Security_Level,
Security_Security_Desc,
Security_Security_Comment,
Security_Security_Inactive_Ind,
App_ID,
App_Name,
App_Description,
DB,
DBNameApp_ID,
For_One_EVA_List_Ind,
#A_ID as Assignee_ID
From v_Security_ALL
Where Security_User_ID = #UID
END
My problem is that the intellsense only sees the first set of return values in the IF statement and I can not access anything from the "else" part of my stored procedure. so when I try to do this:
var apps = dataContext.sp_SELECT_Security_ALL(userId);
foreach (var app in apps)
{
string i = app.
}
On the app. part the only available values I have there is the results of the the first Select distinct above.
Is it possible to use LINQ with this type of stored procedure?
Scott Guthrie has covered this case in a blog post. Scroll down to "Handling Multiple Result Shapes from SPROCs."
The problem isn't with Intellisense. dataContext.sp_SELECT_Security_ALL() is returning a fixed data type. You may be hiding that behind a "var", but it's nevertheless a concrete type with a fixed number of properties. There is still C# remember, and a function can only return one type of object. Look in your dataContext.designer.cs file to see how it's actually defined.
The quick and dirty way to fix this is to coerce every returning statement to return the same thing:
IF #theSkyIsBlue
SELECT CustomerNumber, CustomerName, null as OrderNumber, null as OrderName
FROM Customers
ELSE
SELECT null as CustomerNumber, null as CustomerName, OrderNumber, OrderName
FROM Orders
You may have to watch/(manually change) the nullability of properties in the mapped type, but this will get you where you're going.

Categories