[英]DataServiceContext.SaveChanges() throws Microsoft.OData.Client.DataServiceRequestException
In my Web App I am trying to update my Album
.在我的 Web 应用程序中,我正在尝试更新我的Album
。 To do so, I have an OData Web API.为此,我有一个 OData Web API。 I added the OData Web API service to my Web App using this tutorial .我使用本教程将 OData Web API 服务添加到我的 Web 应用程序。
I am having a problem with calling the DataServiceContext.SaveChanges()
after I called the DataServiceContext.UpdateObject
.我有调用问题DataServiceContext.SaveChanges()
后,我打电话给DataServiceContext.UpdateObject
。 This is the error I receive:这是我收到的错误:
I also use DataServiceContext.SaveChanges()
in other actions (Get and Delete), and there everything works fine.我还在其他操作(获取和删除)中使用DataServiceContext.SaveChanges()
,并且一切正常。 I just get this error in my Edit action.我只是在我的编辑操作中收到此错误。
Microsoft.OData.Client.DataServiceRequestException
HResult=0x80131509
Message=An error occurred while processing this request.
Source=Microsoft.OData.Client
StackTrace:
at Microsoft.OData.Client.SaveResult.HandleResponse()
at Microsoft.OData.Client.BaseSaveResult.EndRequest()
at Microsoft.OData.Client.DataServiceContext.SaveChanges(SaveChangesOptions options)
at biz.dfch.CS.Examples.SampleAspNetCoreWebApp.Controllers.AlbumsController.EditAlbum(Album album) in C:\src\biz.dfch.CS.Examples.SampleAspNetCoreWebApp\src\biz.dfch.CS.Examples.SampleAspNetCoreWebApp\Controllers\AlbumsController.cs:line 64
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
Inner Exception 1:
DataServiceClientException: NotFound
This is my Web App Controller:这是我的 Web 应用程序控制器:
public class AlbumsController : Controller
{
private Container container;
public AlbumsController()
{
container = new Container(new Uri("https://localhost:44316/odata/"));
}
public IActionResult EditAlbum(Album album)
{
container.AttachTo("Albums", album);
container.UpdateObject(album);
container.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
return RedirectToAction(nameof(Index));
}
}
This is my Web API controller:这是我的 Web API 控制器:
public class AlbumsController : ODataController
{
[HttpPut]
public IActionResult UpdateAlbum([FromODataUri] int key,[FromBody] Album album)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != album.Id)
{
return BadRequest();
}
_context.Update(album);
try
{
_context.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!AlbumExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(album);
}
private bool AlbumExists(int key)
{
return _context.Albums.Any(a => a.Id == key);
}
}
This is my view:这是我的观点:
@model biz.dfch.CS.Examples.SampleAspNetCoreWebApi.Models.Album
<div class="row">
<div class="col-md-4">
<form asp-action="EditAlbum">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
This is the Album model:这是专辑模型:
public class Album
{
public Album()
{
Songs = new List<Song>();
}
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Song> Songs { get; set; }
}
The exception you got points to the problem in the connection (DataServiceClientException: NotFound).您得到的异常指向连接中的问题 (DataServiceClientException: NotFound)。 It could mean that the service you told it to connect to is not reachable under a given URL.这可能意味着在给定的 URL 下无法访问您告诉它连接的服务。
Are you 100% certain your service is running on https://localhost:44316/odata/
?您是否 100% 确定您的服务正在https://localhost:44316/odata/
? My guess is that it is running on that port, but not under https
.我的猜测是它在该端口上运行,但不在https
下运行。 Try changing the address to: http://localhost:44316/odata/
and see if that works.尝试将地址更改为: http://localhost:44316/odata/
并查看是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.