当前位置: 首页 > 知识库问答 >
问题:

.NET 3.1:“更新条目时出错。有关详细信息,请参阅内部异常“

郭修平
2023-03-14

我尝试创建包含映像的房间的日志,但它报告了以下错误:错误500Microsoft.EntityFrameworkCore.DBUPDateException:更新条目时出错。有关详细信息,请参阅内部异常。

这是控制器

[HttpPost]
public async Task<IActionResult> Create([FromForm]RoomCreateRequest request)
    {
        var roomId = await _manageRoomService.Create(request);
        if (roomId == null)
            return BadRequest();

        var room = await _manageRoomService.GetById(roomId, request.LanguageId);

        return CreatedAtAction(nameof(GetById), new { id = roomId }, room);
    }

这是服务

public async Task<string> Create(RoomCreateRequest request)
    {
        var room = new Room()
        {
            Id = request.Id,
            Price = request.Price,
            NumberRoom = request.NumberRoom,
            Status = RoomStatus.Empty,
            RoomTranslations = new List<RoomTranslation>() {
                new RoomTranslation()
                {
                    Name = request.Name,
                    Description = request.Description,
                    SeoDescription = request.SeoDescription,
                    SeoTitle = request.SeoTitle,
                    SeoAlias = request.SeoAlias,
                    LanguageId = request.LanguageId
                }
            }
        };

        // Save Image
        if (request.ThumbnailImage != null)
        {
            room.RoomImages = new List<RoomImage>()
            {
                new RoomImage()
                {
                    Caption = "Thumbnail image",
                    DateCreated = DateTime.Now,
                    FileSize = request.ThumbnailImage.Length,
                    ImagePath = await this.SaveFile(request.ThumbnailImage),
                    IsDefault = true,
                    SortOrder = 1
                }
            };                
        }

        _context.Rooms.Add(room);            
        await _context.SaveChangesAsync();
        return room.Id;
    }

保存文件图像方法

// SaveFile Image
    private async Task<string> SaveFile(IFormFile file)
    {
        var originalFileName = 
        ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
        var fileName = $"{Guid.NewGuid()}{Path.GetExtension(originalFileName)}";
        await _storageService.SaveFileAsync(file.OpenReadStream(), fileName);
        return fileName;
    }

文件服务

public async Task SaveFileAsync(Stream mediaBinaryStream, string fileName)
    {
        var filePath = Path.Combine(_userContentFolder, fileName);
        using var output = new FileStream(filePath, FileMode.Create);
        await mediaBinaryStream.CopyToAsync(output);
    }`

Class RoomCreateRequest`public Class RoomCreateRequest{public string Id{set;Get;}public decimal Price{set;Get;}public string NumberRoom{set;Get;}public string Name{set;Get;}

    public string Description { set; get; }
    public string SeoDescription { set; get; }
    public string SeoTitle { set; get; }

    public string SeoAlias { get; set; }
    public string LanguageId { set; get; }

    public IFormFile ThumbnailImage { get; set; }
 }

共有1个答案

羿宏硕
2023-03-14

异常的顶部显示无效列名'isdefault'。为了解决此问题,您应该在room表中有一个名为isdefault的列。

※我假设您的表根据您的上下文对象命名为room,在您的情况下可能会有所不同

 类似资料: