[EnableQuery(PageSize=20)]和之间有什么区别[EnableQuery(MaxTop=20)]?
据我所知,他们两个都为结果设置了最大限制。
调用GET odata/Products?$top=100它们两个都只给我20个结果。
标题说明了一切.我发现了几个不同方式的博客(将EF模型序列化为XML,然后再次反序列化为IEdmModel),但它们都基于旧版本的OData包.
尝试使用主键CourseID针对odata web.api查找单个记录,方法如下:
var editedcourse = container.Courses.Where(c => c.CourseID == ID).SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
这是错误的:
<m:innererror>
<m:message>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/atom+xml; charset=utf-8'.</m:message>
<m:type>System.InvalidOperationException</m:type>
<m:stacktrace></m:stacktrace>
<m:internalexception>
<m:message>'SingleResult`1' cannot be serialized using the ODataMediaTypeFormatter.</m:message>
<m:type>System.Runtime.Serialization.SerializationException</m:type>
Run Code Online (Sandbox Code Playgroud) wcf-data-services odata asp.net-web-api asp.net-web-api-odata
我必须使用子列表更新我的实体,如下所示:
public class Entity1
{
int Id{get;set;}
ObservableCollection<Child1> ChildrenList {get;set;}
string Name{get;set;}
}
public class Child1
{
string Nome{get;set;}
string Cognome {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式实现了补丁方法:
[AcceptVerbs("PATCH", "MERGE")]
public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Entity1> entityDelta)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var entity = await Context.Entity1.FindAsync(key);
if (entity == null)
{
return NotFound();
}
entityDelta.Patch(entity);
try
{
await Context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
return NotFound();
}
return Updated(entity);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试以这种方式从提琴手调用它时:
Url: http://localhost/odata4/Entity1(1)/ with patch request
Request Headers: Content-Type: application/json
Request …Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,我.svc对ODATA服务的后缀只有一点混淆,因为我看到的大多数示例.svc在根URL上总是有后缀,例如:
http://services.odata.org/V4/Northwind/Northwind.svc/
甚至在ODATA文档示例上:

在ASP.NET Web Api上构建ODATA时,我发现后缀.svc实际上并不是强制性的,如果我们想要路由配置,我们可以添加它.
那么.svc为了构建ODATA服务,为根URL 添加后缀的目的是什么呢?或者只是指定这是ODATA 服务的约定?
在这个简单的例子中,我试图从Web Api 2 + OData v4服务获取一个序列化为JSON的对象.控制器具有绑定功能Test,它返回一个annon数组.对象.
public class ProductsController : ODataController
{
[HttpGet]
public IHttpActionResult Test(int key)
{
var res = new[]
{
new { Name = "a", Value = new[] { 1, 2, 3 } },
new { Name = "b", Value = new[] { 2, 4, 5 } }
// this also produces same result
// new { Name = "a", Value = "c" },
// new { Name = "b", Value = "c" }
};
return this.Ok(res); …Run Code Online (Sandbox Code Playgroud) 我正在使用带有web api的Odata并收到此错误:
<m:innererror>
<m:message>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.
</m:message>
<m:type>System.InvalidOperationException</m:type>
<m:stacktrace/>
<m:internalexception>
<m:message>
No IdLink factory was found. Try calling HasIdLink on the EntitySetConfiguration for 'Tag'.
</m:message>
<m:type>System.InvalidOperationException</m:type>
Run Code Online (Sandbox Code Playgroud)
这是我的OData配置:
config.Routes.MapODataRoute("ODataRoute", "odata", CreateModel());
static IEdmModel CreateModel()
{
var modelBuilder = new ODataModelBuilder();
modelBuilder.EntitySet<Tag>("Tag");
modelBuilder.EntitySet<Post>("Post");
return modelBuilder.GetEdmModel();
}
Run Code Online (Sandbox Code Playgroud)
这是我的OData控制器
public class TagController : EntitySetController<Tag, int>
{
private readonly IUnitOfWork _unitOfWork;
public TagController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public override IQueryable<Tag> Get() …Run Code Online (Sandbox Code Playgroud)