Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call is failing with null parameter

I have the following code:

public static ContactEventValue GetContactEventValue(ContactEventType contactEventType, string programCode, string brandCode)
{
    AdvocacyEntities ent = AdvocacyEntities.GetReadOnlyInstance();
    ContactEventValue value = ent.ContactEventValues.SingleOrDefault(
        x => x.ContactEventTypeID == contactEventType.ContactEventTypeID
        && x.ProgramCode == programCode && x.BrandCode == brandCode);
}

When I call it with values for brandCode and programCode, I get the expected value back from the database. When I make the call but explicitly setting x.ProgramCode and x.BrandCode to null I get the expected default value back from the database:

ContactEventValue value = ent.ContactEventValues.Single(
        x => x.ContactEventTypeID == contactEventType.ContactEventTypeID
        && x.ProgramCode == null && x.BrandCode == null);

However, when I call the method with null for programCode and brandCode, I get null back from the database!

I tried changing the == to .Equals() per the answer to this issue: Nullable optional parameter

So x.BrandCode.Equals(brandCode) replaced x.BrandCode == brandCode, and x.ProgramCode.Equals(programCode) replaced x.ProgramCode == programCode, but that still didn't work.

I also tried using the ?? operator, still didn't work.

This issue says an solution wasn't found, and s/he had to used a stored procedure: EF 4 Query - Issue with Multiple Parameters I really don't want to have to go there.

Any ideas?

like image 914
jgerman Avatar asked Apr 30 '13 22:04

jgerman


People also ask

What is the meaning of Param null?

param IS NULL is the first conditional so either this is true or the second conditional value = param is true.

What does value Cannot be null parameter name source mean?

​​​Value cannot be null. Parameter name: InString: This error indicates that an item failed to migrate because it has corrupt MAPI properties. More specifically, Exchange Web Services (EWS) returned a MAPI property whose value should always be a non-empty string, but was returned as an empty string.

Can you pass null parameter in Java?

You cannot pass the null value as a parameter to a Java scalar type method; Java scalar types are always non-nullable. However, Java object types can accept null values. The values of both variable <@I> and variable <@A> are null, since values have not been assigned to them.


2 Answers

I don't know what version of EF you're using, but null comparison was an issue prior to version 5. If you inspect the SQL that is actually being emitted, you'll probably see that IS NULL is not used in the query.

In EF 6, you'll be able to set the UseDatabaseNullSemantics configuration option exposed on DbContext:

public class MyContext : DbContext
{
    public MyContext()
    {
        this.Configuration.UseDatabaseNullSemantics = true;
    }
}

For EF 5, you can use the UseCSharpNullComparisonBehavior setting on the underlying ObjectContext:

public class MyContext : DbContext
{
    public MyContext()
    {
        var objectContextAdapter = this as IObjectContextAdapter;
        objectContextAdapter.
            ObjectContext.ContextOptions.UseCSharpNullComparisonBehavior = true;    
    }
}

However, you will need to use .NET Framework 4.5 for your project. If you don't wish to use 4.5, then you can use one of the workarounds listed at How can i query for null values in entity framework?.

like image 61
devdigital Avatar answered Sep 28 '22 07:09

devdigital


Turns out the ?? operator solution does work, I simply didn't apply it to both sides of the == statements. So the following code solves the problem:

public static ContactEventValue GetContactEventValue(ContactEventType contactEventType, string programCode, string brandCode) {
    AdvocacyEntities ent = AdvocacyEntities.GetReadOnlyInstance();
    ContactEventValue value = ent.ContactEventValues.SingleOrDefault(
            x => x.ContactEventTypeID == contactEventType.ContactEventTypeID
            && (x.ProgramCode ?? "") == (programCode ?? "")
            && (x.BrandCode ?? "") == (brandCode ?? ""));

However, this causes empty string and null to be equivalent. Not ideal.

like image 40
jgerman Avatar answered Sep 28 '22 07:09

jgerman