通过null参数调用失败 [英] Call is failing with null parameter

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

问题描述

我有以下代码:

  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我从数据库中得到预期的默认值:

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

但是,当我为programCode和brandCode调用null的方法时,我从数据库中返回null !



我尝试将==改为.Equals(),以解决此问题: Nullable可选参数



所以x.BrandCode.Equals(brandCode)替换x.BrandCode = = brandCode和x.ProgramCode.Equals(programCode)替换x.ProgramCode == programCode,但仍然没有工作。



我也试过使用??运营商仍然没有工作。



这个问题说没有找到解决方案,他/他不得不使用一个存储过程: EF 4查询 - 具有多个参数的问题我真的不想去那里。



任何想法?

解决方案

我不知道什么版本的EF使用,但空比较是第5版之前的问题。如果您检查实际发出的SQL,则可能会看到 IS NULL 在$ 6

在EF 6中,您可以设置 UseDatabaseNullSemantics 配置选项公开在 DbContext

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

对于EF 5,您可以使用 UseCSharpNullComparisonBehavior 设置在底层的 ObjectContext

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

但是,您将需要使用.NET Framework 4.5为您的项目。如果您不希望使用4.5,那么您可以使用如何在实体框架中查询空值?


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?

解决方案

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?.

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

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