3

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:

  1. Create SP
  2. Update Model (edmx)
  3. Add new function import
  4. Generate new complex type by clicking the button
  5. Run Custom Tool on separate template file (to generate POCO)
5
  • How can you pass NULL to varchar(30) unless it is an optional parameter? Commented May 13, 2011 at 7:58
  • Doesn't it give error that it is expecting a non null parameter at runtime? Commented May 13, 2011 at 17:22
  • @Akash - Yes, because the generated complex type has this parameter as string instead of "string?", hence the question. Commented May 13, 2011 at 18:54
  • @subt13 -1, please read little about C# strings, String is by default nullable, if you can prove string? exists or its even possible, I will remove my downvote. Commented May 15, 2011 at 11:15
  • 1
    @Akash - Ya, I realize that there is no string? and that string is already nullable. BTW, if you have an answer to this question feel free to post it instead of spamming me to death. Commented May 15, 2011 at 14:26

4 Answers 4

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I could actually just add an additional bit param and have two code paths in the same sp, probably cleaner than two separate ones...
1

Quick look and I found this on stackoverflow. Hope it helps.

Entity Framework 4.0 Entity SQL passing null ObjectParameter parameters

Comments

1

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.

Comments

0

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)

5 Comments

Doesn't work. -1 for not even attempting it before guessing.
@subt13, somebody is trying to help you, you are not paying anyone to help you here so please try to say thank you for help, no one is going to stop their work and attempt your solution because it is your job !! not Zeno's. This isne a work place where Zeno will get money for telling you solution.
@subt13.. I faced something similar issue once and it got resolved by setting default parameter in stored procedure itself that's why I shared it. I didn't have any environment set up in my home computer so didn't get a chance to test it. I am sorry for the time you have wasted while testing this. @Akash.. thanks for the support
@subt13, Well String is already nullable, what more do you want? I was stupid to ignore the String parameter type but now I realize, you can always pass null to string parameter.
@ZeNo - You might mention that next time in your solution or provide some proof that it works. I had already tried this before you even posted your "answer" and it doesn't work. Maybe now you can see why it appears to be just a "guess" that doesn't work and will lead to wasted time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.