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

ASP.NET Core 3.1:System.InvalidCastException:无法将“System.Int32”类型的对象转换为“System.Int64”类型

如何解决ASP.NET Core 3.1:System.InvalidCastException:无法将“System.Int32”类型的对象转换为“System.Int64”类型

Picture Error

我的控制器:

[HttpDelete("{roomId}")]
    public async Task<IActionResult> Delete(string roomId)
    {
        var affectedResult = await _manageRoomService.Delete(roomId);
        if (affectedResult == 0)
            return BadRequest();

        return Ok();
    }

我的服务:我尝试调试,但它在代码foreach (var image in images) 中停止。

public async Task<int> Delete(string roomId)
{
        var room = await _context.Rooms.FindAsync(roomId);

        if (room == null) 
             throw new EWebHotelException($"Cannot find a room: {roomId}");

        var images = _context.RoomImages.Where(i => i.RoomId == roomId);

        foreach (var image in images)
        {
            await _storageService.DeleteFileAsync(image.ImagePath);
        }
        
        _context.Rooms.Remove(room);

        return await _context.SaveChangesAsync();
}

我在 FileStorageService.cs 中的方法

public async Task DeleteFileAsync(string fileName)
{
        var filePath = Path.Combine(_userContentFolder,fileName);

        if (File.Exists(filePath))
        {
            await Task.Run(() => File.Delete(filePath));
        }
}     

在 RoomImages 表中,列 Id 的类型为 int 32。

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