1
我有一個OData服務使用WebAPI和EF 6構建。我使用流暢的api來創建我的模型。它工作正常,但是當我使用$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>();
}
}
甲Customer
不需要有一個地址。如果我查詢它像/customers?$expand=Address
它不會包括Customers
其中Address == null
。它只會返回存在Customer.Address
的Customer
實體。
我想它做了一個內部連接,因此沒有得到沒有Address
的Customer
實體。 有沒有任何方法可以包含地址爲空的客戶?
電流輸出:
[
{
Id: 1,
Name: 'Customer1',
AddressId: 1,
Address :
{
Id: 1,
Street: 'Some street'
}
}
]
求購輸出:上述
[
{
Id: 1,
Name: 'Customer1',
AddressId: 1,
Address :
{
Id: 1,
Street: 'Some street'
}
},
{
Id: 2,
Name: 'Customer2',
AddressId: null,
Address : null
}
]
這些模型是一個例子。實際上我有更大的模型,但我試圖儘可能縮短範例以提供mcve。我已閱讀this和this問題,但我沒有收到任何錯誤。我根本沒有得到這些實體。
你的EF查詢是什麼? – Sampath
我只是將'DbSet'作爲查詢返回,所以我根本不做任何查詢。我將代碼添加到示例中。 – smoksnes