呼叫与空参数失败 [英] Call is failing with null parameter

查看:182
本文介绍了呼叫与空参数失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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);
}

当我与brandCode和programCode值调用它,我得到的预期值从数据库返回的。当我拨打电话,但明确设置x.ProgramCode和x.BrandCode为null,我得到预期的默认值从数据库返回的:

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);



然而,当我打电话与空的programCode和brandCode的方法,我从数据库中获取空回!

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

我试图改变==每答案.Equals()这个问题:的 LINQ到实体为空的可选参数

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

所以x.BrandCode.Equals(brandCode)代替点¯x .BrandCode == brandCode和x.ProgramCode.Equals(programCode)取代x.ProgramCode == programCode,但是这仍然没有奏效。

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.

这问题说,一个解决方案没有被发现,和他/她不得不用一个存储过程:的 EF 4查询 - 与多参数文档我真的不希望有去那里。

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.

任何想法?

推荐答案

我不知道是什么版本EF的你使用,但空比较是前5.如果您检查,实际上是被发射的SQL版本的问题,你可能会看到 IS NULL 是不是在查询中使用。

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.

在EF 6,您就可以将 UseDatabaseNullSemantics 配置选项暴露在的DbContext

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;
    }
}

有关EF 5,你可以使用 UseCSharpNullComparisonBehavior 于底层的ObjectContext 设置:

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;    
    }
}



不过,您将需要使用.NET框架4.5为您的项目。如果你不希望使用4.5,那么你可以在使用的如何在实体框架?

这篇关于呼叫与空参数失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆