Files.EntityFrameworkCore.Extensions 2.3.0

dotnet add package Files.EntityFrameworkCore.Extensions --version 2.3.0                
NuGet\Install-Package Files.EntityFrameworkCore.Extensions -Version 2.3.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Files.EntityFrameworkCore.Extensions" Version="2.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Files.EntityFrameworkCore.Extensions --version 2.3.0                
#r "nuget: Files.EntityFrameworkCore.Extensions, 2.3.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Files.EntityFrameworkCore.Extensions as a Cake Addin
#addin nuget:?package=Files.EntityFrameworkCore.Extensions&version=2.3.0

// Install Files.EntityFrameworkCore.Extensions as a Cake Tool
#tool nuget:?package=Files.EntityFrameworkCore.Extensions&version=2.3.0                

Files.EntityFrameworkCore.Extensions

This is a library for storing files in small chunks on sql using EntityFrameworkCore. Works well with Entity Framework as an extension.

Get Started

Install-Package Files.EntityFrameworkCore.Extensions

Example

More examples found inside the repository

  
  	public class UserImage : IFileEntity
	{
		public Guid Id { get; set; }
		public Guid FileId { get; set; }
		public string Name { get; set; }
		public string MimeType { get; set; }
		public DateTimeOffset TimeStamp { get; set; }
		public Guid? NextId { get; set; }
		public int ChunkBytesLength { get; set; }
		public long TotalBytesLength { get; set; }
		public byte[] Data { get; set; }
	}
	
	public class UploadFileIdCommand
    	{
        	public Guid? FileId { get; set; }
    	}
	
	public class UploadCommand
	{
		public IFormFile File { get; set; }
	}
  
  	public class WebApiContext : DbContext
	{
		public WebApiContext(DbContextOptions<WebApiContext> options)
			: base(options)
		{
		}
		public DbSet<UserImage> UserImage { get; set; }
	}
  
        [Route("api/user-images")]
	[ApiController]
	public class UserImagesController : ControllerBase
	{
		private readonly WebApiContext _context;

		public UserImagesController(WebApiContext context)
		{
			_context = context;
		}

		[HttpPost]
		[DisableRequestSizeLimit]
		public async Task<ActionResult<FilesExtensionsResponse>> UploadFile([FromForm] UploadCommand uploadCommand)
		{
			var file = uploadCommand.File;
			if (file.Length > 0)
			{
				var fileDetails = await _context.SaveFileAsync<UserImage>(file.OpenReadStream(), file.FileName, file.ContentType);
				return Ok(fileDetails);
			}
			else
			{
				return BadRequest("File is required.");
			}
		}
		
		[HttpPost("other-file")]
        	public async Task<ActionResult<FilesExtensionsResponse>> UploadOtherFile([FromBody] UploadFileIdCommand command)
        	{
			//The @"appsettings.json" is a path to any file you will like to save
            		var fileDetails = await _context.SaveFileAsync<UserImage>(@"appsettings.json", command?.FileId);
			//Save will be auto called on every chunk addition so that memory usage remain low, i.e. await _context.SaveChangesAsync();
            		return Ok(fileDetails);
        	}

		[HttpGet("{id}/download")]
		public async Task<IActionResult> DownLoadFile(Guid id)
		{
			var fileDetails = await _context.GetFileInfoAsync<UserImage>(id);
			var stream = new MemoryStream();
			await _context.DownloadFileToStreamAsync<UserImage>(id, stream);
			return File(stream, fileDetails.MimeType, fileDetails.Name);
		}

		[HttpGet("{id}/view")]
		public async Task<FileStreamResult> DownloadView(Guid id)
		{
			var fileDetails = await _context.GetFileInfoAsync<UserImage>(id);
			var stream = new MemoryStream();
			await _context.DownloadFileToStreamAsync<UserImage>(id, stream);
			return new FileStreamResult(stream, fileDetails.MimeType);
		}

		[HttpDelete("{id}")]
		public async Task<IActionResult> DeleteUserImage(Guid id)
		{
			if (_context.UserImage == null)
			{
				return NotFound();
			}
			var userImage = await _context.UserImage.FindAsync(id);
			if (userImage == null)
			{
				return NotFound();
			}

			await _context.DeleteFileAsync<UserImage>(id);
			//Save will be auto called on every chunk deletion so that memory usage remain low, i.e. await _context.SaveChangesAsync();

			return NoContent();
		}
	}

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.3.0 3,449 9/2/2023
2.2.0 327 5/28/2023
2.1.0 143 5/13/2023
2.0.0.1 144 5/6/2023