Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OData$expand不包括展开属性为null的实体_C#_Entity Framework_Asp.net Web Api_Odata - Fatal编程技术网

C# OData$expand不包括展开属性为null的实体

C# OData$expand不包括展开属性为null的实体,c#,entity-framework,asp.net-web-api,odata,C#,Entity Framework,Asp.net Web Api,Odata,我有一个使用WebAPI和EF6构建的OData服务。我使用fluentapi创建模型。它工作正常,但是当我使用$expand时,它会忽略展开属性为null的实体 这是一个简化的示例: public class Customer { public int Id { get; set; } public string Name { get; set; } public int? AddressId { get set; } // Note that Address is o

我有一个使用WebAPI和EF6构建的OData服务。我使用fluentapi创建模型。它工作正常,但是当我使用
$expand
时,它会忽略展开属性为
null
的实体

这是一个简化的示例:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? AddressId { get set; } // Note that Address is optional
    public Address Address { get; set; }
}

public class Address
{
    public int Id { get; set; }
    public string Street{ get; set; }
}


public class CustomersController : ODataController
{
    [EnableQuery]
    public virtual IQueryable<Customer> Get(ODataQueryOptions<Customer> q)
    {
        // I return IQueryable from DBSet here...
        return db.GetDbSet<Customer>();
    }
}
想要的输出:

[
    {
        Id: 1,
        Name: 'Customer1',
        AddressId: 1,
        Address : 
        {
            Id: 1,
            Street: 'Some street'
        }
    },
    {
        Id: 2,
        Name: 'Customer2',
        AddressId: null,
        Address : null
    }
]

上面的模型就是一个例子。实际上,我得到了更大的模型,但我试着尽可能短的例子来提供一个更好的解释。我已经阅读并提问了,但我没有发现任何错误。我根本没有得到实体。

使用
$expand
时,想要的输出似乎是OData服务的正常行为。错误是由我的映射引起的

出于某种原因,我使用了一些旧代码,使得
地址成为必需的,即使外键本身是可以为空的

public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        // Other code...

        // This is wrong!
        this.HasReguired(c => c.Address).WithMany(a => a.Customers);


        // This is right!
        this.HasOptional(c => c.Address).WithMany(a => a.Customers); 
    }
}
公共类CustomerMap:EntityTypeConfiguration
{
公共客户映射()
{
//其他代码。。。
//这是错误的!
this.hasrequired(c=>c.Address).WithMany(a=>a.Customers);
//这是对的!
this.has可选(c=>c.Address)。有许多(a=>a.Customers);
}
}

由于映射,EF将SQL转换为
内部连接
,而不是
外部连接

使用
$expand
时,想要的输出似乎是OData服务的正常行为。错误是由我的映射引起的

出于某种原因,我使用了一些旧代码,使得
地址成为必需的,即使外键本身是可以为空的

public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public CustomerMap()
    {
        // Other code...

        // This is wrong!
        this.HasReguired(c => c.Address).WithMany(a => a.Customers);


        // This is right!
        this.HasOptional(c => c.Address).WithMany(a => a.Customers); 
    }
}
公共类CustomerMap:EntityTypeConfiguration
{
公共客户映射()
{
//其他代码。。。
//这是错误的!
this.hasrequired(c=>c.Address).WithMany(a=>a.Customers);
//这是对的!
this.has可选(c=>c.Address)。有许多(a=>a.Customers);
}
}

由于映射,EF将SQL转换为
内部连接
,而不是
外部连接

您的
EF查询是什么
?我只是将我的
DbSet
作为查询表返回,所以我根本不做查询。我将代码添加到示例中。您的
EF查询是什么
?我只是将我的
DbSet
作为可查询项返回,所以我根本不进行查询。我将代码添加到示例中。