C# OData$expand不包括展开属性为null的实体
我有一个使用WebAPI和EF6构建的OData服务。我使用fluentapi创建模型。它工作正常,但是当我使用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
$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
作为可查询项返回,所以我根本不进行查询。我将代码添加到示例中。