【问题标题】:.NET 3.1: "An error occurred while updating the entries. See the inner exception for details".NET 3.1:“更新条目时发生错误。有关详细信息,请参阅内部异常”
【发布时间】:2021-04-09 00:45:54
【问题描述】:

我尝试创建包含该图像的房间的日志,但它报告了此错误

Error 500
Microsoft.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;
    }

这是 FileStorageService 代码块await mediaBinaryStream.CopyToAsync(output); 将复制名称文件并在“SaveFile”方法中添加到文件名,但在调试时它不起作用。

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

这是教室创建请求

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

但如果我不添加图像,它会起作用。

【问题讨论】:

  • 内部异常是什么?
  • 基于包含错误的链接图像 - 与数据库存在某种不匹配。您是否忘记运行一些迁移?

标签: c# asp.net-core


【解决方案1】:

在异常顶部显示Invalid Column name 'IsDefault'。为了解决这个问题,您的Room 表中应该有一个名为IsDefault 的列。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多