简体   繁体   English

DataServiceContext.SaveChanges() 抛出 Microsoft.OData.Client.DataServiceRequestException

[英]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.

相关问题 C# OdataV4 Microsoft.OData.Client.DataServiceContext 处理时区 - C# OdataV4 Microsoft.OData.Client.DataServiceContext handle Timezone Microsoft OData客户端 - Microsoft OData Client 如何在客户端(DataServiceContext)加密OData请求有效负载并在ServerSide解密请求? - How could I Encrypt OData Request Payload at Client Side(DataServiceContext) and Decrypting Request at ServerSide? WCF 中的 Dataservicecontext 服务-插入速度很慢 savechanges() - Dataservicecontext in WCF Service- insertion is slow savechanges() 没有DataServiceKey属性的OData DataServiceContext查询 - OData DataServiceContext query without DataServiceKey attribute 支持5.2.0.0的WinRT DataServiceContext的服务参考(Microsoft.Data.Services.Client.WindowsStore) - Service reference for WinRT DataServiceContext that supports 5.2.0.0 (Microsoft.Data.Services.Client.WindowsStore) 实体SaveChanges引发异常 - Entities SaveChanges throws exception .NET CORE 5 中的 Microsoft OData - 将 OData 添加到服务会引发丢失的 using 指令,但包在那里 - Microsoft OData in .NET CORE 5 - Adding OData to services throws up a missing using directive yet the package is there Microsoft.OData.Client是否更改PATCH请求的服务端口? - Does Microsoft.OData.Client change the service port on PATCH requests? 阻止Microsoft OData Client请求完整的元数据 - Prevent Microsoft OData Client from requesting Full Metadata
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM