aar*_*ark 5 c# entity-framework odata asp.net-web-api asp.net-mvc-viewmodel
这个问题非常相似,但没有给我我所需要的。
我正在使用Entity Framework6。我的数据库有两个表,Customers和CustomerTypes。我为每个创建了一个ViewModel。客户可以具有以下类型:
public class Customer
{
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public CustomerTypeViewModel CustomerType { get; set; }
}
public class CustomerTypeViewModel
{
public int CustomerTypeID { get; set; }
public string CustomerTypeDescription { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有一个Customer控制器,它公开了具有IQueryable返回类型的odata操作方法:
[HttpPost, Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable<CustomerViewModel> GetCustomersMatchingCriteria([FromBody]ODataActionParameters parameters)
{
var criteria = (CustomerMassUpdateCriteriaViewModel)parameters["Criteria"];
return Common.Customers.GetCustomerMassUpdateCriteriaResults(criteria,
ConfigurationManager.AppSettings["CLIENT_ID"]).Select(
c => new CustomerViewModel()
{
CustomerID = c.CustomerID,
CustomerName = c.CustomerName,
CustomerType = new CustomerTypeViewModel()
{
CustomerTypeDescription = c.CustomerType.CustomerTypeDescription
}
});
}
Run Code Online (Sandbox Code Playgroud)
Common.Customers.GetCustomerMassUpdateCriteriaResults方法仅返回一个IQueryable of Customer,它是实际的实体。
问题是,使用以下查询字符串选项调用此控制器方法时:
$expand=CustomerType
$select=CustomerID,CustomerName,CustomerType/CustomerTypeDescription
Run Code Online (Sandbox Code Playgroud)
抛出此异常:
'ObjectContent`1'类型未能序列化内容类型'application / json的响应主体;charset = utf-8'。“,”类型“:” System.InvalidOperationException“
DbIsNullExpression的参数必须引用基本,枚举或引用类型。
从$ select列表中删除$ expand选项和关联的CustomerType / CustomerTypeDescription属性不会产生任何错误。
我觉得这里缺少明显的东西。有任何想法吗?
第一次编辑:
通过ToList()扩展方法枚举结果并返回IEnumerable而不是IQueryable成功地扩展了CustomerType导航属性,但是在数据库级别不再尊重我的ODATA $ select列表。这是否违反了使用ODATA的目的?
小智 0
例外 :
“ObjectContent`1”类型无法序列化内容类型“application/json”的响应正文;charset=utf-8'.","type":"System.InvalidOperationException"
据我所知,这是由于请求如何达到 OData 格式所致。该请求应到达 OData 路由进行格式化。如果您可以在 global.asax 中的RouteConfig.RegisterRoutes和WebApiConfig.Register之前移动GlobalConfiguration.Configuration.EnableOData()可能会有所帮助。
归档时间: |
|
查看次数: |
2059 次 |
最近记录: |