微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

DataServiceContext.SaveChanges引发Microsoft.OData.Client.DataServiceRequestException

如何解决DataServiceContext.SaveChanges引发Microsoft.OData.Client.DataServiceRequestException

在我的Web应用程序中,我正在尝试更新我的Album。为此,我有一个OData Web API。我使用this tutorial将OData Web API服务添加到了Web应用程序中。

调用DataServiceContext.SaveChanges()之后,调用DataServiceContext.UpdateObject时遇到问题。这是我收到的错误

我还在其他操作(“获取”和“删除”)中使用了DataServiceContext.SaveChanges(),一切正常。我只是在“编辑”操作中收到此错误

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

这是我的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));
        }
    }

这是我的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);
        }
    }

这是我的观点:

@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>

这是相册模型:

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; }
}

解决方法

您获得的异常指向连接中的问题(DataServiceClientException:NotFound)。这可能意味着您指定要连接的服务在给定的URL下无法访问。

您是否100%确定服务在https://localhost:44316/odata/上运行? 我的猜测是它正在该端口上运行,但不在https下运行。 尝试将地址更改为:http://localhost:44316/odata/,看看是否可行。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。