{"id":22462471,"url":"https://github.com/samanazadi1996/sam.filetablesqlserver","last_synced_at":"2025-08-02T05:31:43.617Z","repository":{"id":45055114,"uuid":"435633804","full_name":"samanazadi1996/Sam.FileTableSqlServer","owner":"samanazadi1996","description":"A simple yet powerful library for managing files in ASP.NET Core using SQL Server FileTable, supporting CRUD operations and complex queries.","archived":false,"fork":false,"pushed_at":"2024-11-25T13:49:33.000Z","size":149,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-25T14:43:55.042Z","etag":null,"topics":["csharp","dotnet","filetable","framework","sql-server"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/Sam.FileTableFramework","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samanazadi1996.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"lfx_crowdfunding":null,"polar":null,"buy_me_a_coffee":"samanazadi","custom":null}},"created_at":"2021-12-06T20:13:03.000Z","updated_at":"2024-11-25T13:49:31.000Z","dependencies_parsed_at":"2024-04-19T18:27:27.963Z","dependency_job_id":"310a1323-cdd2-4cb7-9290-187d3e082449","html_url":"https://github.com/samanazadi1996/Sam.FileTableSqlServer","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samanazadi1996%2FSam.FileTableSqlServer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samanazadi1996%2FSam.FileTableSqlServer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samanazadi1996%2FSam.FileTableSqlServer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samanazadi1996%2FSam.FileTableSqlServer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samanazadi1996","download_url":"https://codeload.github.com/samanazadi1996/Sam.FileTableSqlServer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228439518,"owners_count":17920025,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","filetable","framework","sql-server"],"created_at":"2024-12-06T09:09:50.514Z","updated_at":"2024-12-06T09:10:44.622Z","avatar_url":"https://github.com/samanazadi1996.png","language":"C#","funding_links":["https://buymeacoffee.com/samanazadi"],"categories":[],"sub_categories":[],"readme":"# Building a File Management Application with ASP.NET Core and SQL Server FileTable \n\n[![Badge CI](https://github.com/samanazadi1996/Sam.FileTableSqlServer/workflows/.NET/badge.svg)](https://github.com/samanazadi1996/Sam.FileTableSqlServer/actions)\n[![Badge NuGet](https://img.shields.io/nuget/vpre/Sam.FileTableFramework.svg)](https://www.nuget.org/packages/Sam.FileTableFramework)\n\n# Introduction\n\nIn today's digital world, effective file management is crucial for individuals and organizations alike. Developing a file management application can help streamline file organization and improve accessibility. In this article, we will explore the intricacies and implementation steps of building a file management application using ASP.NET Core and SQL Server FileTable.\n\n# Getting Started\n\n1. You'll want to enable SQL Server FILESTREAM. Follow the instructions in [this link](./Documents/EnableSqlServerFILESTREAM.md) for the necessary guidance on activation.\n\n2. To install the Sam.File Table Framework package, simply use the following command\n   - .NET CLI\n\n        ``` sh\n        dotnet add package Sam.FileTableFramework --version 2.1.0\n        ```\n   - Package Manager\n\n        ``` sh\n        NuGet\\Install-Package Sam.FileTableFramework -Version 2.1.0\n        ```\n\n   - Package Reference\n        ``` xml\n        \u003cPackageReference Include=\"Sam.FileTableFramework\" Version=\"2.1.0\" /\u003e\n        ```\n    \n    - Paket CLI\n        ``` sh\n        paket add Sam.FileTableFramework --version 2.1.0\n        ```\n\n3. Create your DbContext by inheriting from FileTableDbContext. Then, define a FtDbSet property for your tables.\n    ``` c#\n    using Sam.FileTableFramework.Context;\n\n    namespace Sam.Persistence\n    {\n        public class DatabaseContext: FileTableDBContext\n        {\n            public FtDbSet Table1 { get; set; }\n            public FtDbSet Table2 { get; set; }\n            public FtDbSet Table3 { get; set; }\n        }\n    }\n    ```\n    You can have your advanced FtDbSet, for this, refer to [this link](./Documents/CustomFtDbSet.md)\n\n4. Register your DatabaseContext class in Program.cs\n    ``` c#\n    // ...\n    builder.Services.AddFileTableDBContext\u003cDatabaseContext\u003e(o =\u003e\n        o.ConnectionString = \"Data Source =.; Initial Catalog = DatabaseName; Integrated Security = true\");\n    // ...\n\n5. You can create the database by adding the following code when the project is running\n    ``` c#\n    // ...\n    using (var scope = app.Services.CreateScope())\n    {\n        var services = scope.ServiceProvider;\n\n        services.GetRequiredService\u003cDatabaseContext\u003e().Migrate();\n    }\n    // ...\n    ```\n6. Now you can inject DatabaseContext in your classes and use your tables, for example, see the source code below\n    ``` c#\n    using Microsoft.AspNetCore.Mvc;\n    using Sam.EndPoint.WebApi.Models;\n    using Sam.FileTableFramework.Linq;\n    using Sam.Persistence;\n    using System.Net.Mime;\n\n    namespace Sam.EndPoint.WebApi.Controllers\n    {\n        [ApiController]\n        [Route(\"[controller]\")]\n        public class FileController(DatabaseContext databaseContext) : ControllerBase\n        {\n\n            [HttpGet(\"GetPaged/{page}/{pageCount}\")]\n            public async Task\u003cIActionResult\u003e GetPaged(int page, int pageCount)\n            {\n                var skip = (page - 1) * pageCount;\n\n                var query = databaseContext.Table1;\n\n                var result = await query\n                    .Skip(skip)\n                    .Take(pageCount)\n                    .OrderBy(p =\u003e p.name)\n                    .ToListAsync(p =\u003e new FileEntityDto()\n                    {\n                        Name = p.name,\n                        Size = p.cached_file_size,\n                        Id = p.stream_id,\n                        Type = p.file_type\n                    });\n\n                return Ok(result);\n            }\n\n            [HttpGet(\"GetAll\")]\n            public async Task\u003cIActionResult\u003e GetAll()\n            {\n                var query = databaseContext.Table1;\n\n                var result = await query\n                    .ToListAsync(p =\u003e new FileEntityDto()\n                    {\n                        Name = p.name,\n                        Size = p.cached_file_size,\n                        Id = p.stream_id,\n                        Type = p.file_type\n                    });\n\n                return Ok(result);\n            }\n\n            [HttpGet(\"Count\")]\n            public async Task\u003cIActionResult\u003e Count()\n            {\n                var query = databaseContext.Table1;\n                return Ok(await query.CountAsync());\n            }\n\n            [HttpGet(\"Download/{name}\")]\n            public async Task\u003cIActionResult\u003e Download(string name)\n            {\n                var entity = await databaseContext.Table1.Where($\"name = '{name}'\").FirstOrDefaultAsync();\n\n                if (entity is null)\n                    return NotFound(nameof(NotFound));\n\n                return File(entity.file_stream!, MediaTypeNames.Application.Octet, entity.name);\n            }\n\n            [HttpPost(\"Upload\")]\n            public async Task\u003cIActionResult\u003e Upload(IFormFile file)\n            {\n                var fileName = Guid.NewGuid() + file.FileName[file.FileName.LastIndexOf('.')..];\n                var stream = file.OpenReadStream();\n\n                await databaseContext.Table1.CreateAsync(fileName, stream);\n\n                return Ok(fileName);\n            }\n\n            [HttpDelete(\"Delete\")]\n            public async Task\u003cIActionResult\u003e Delete(string name)\n            {\n                var entity = await databaseContext.Table1.Where($\"name = '{name}'\").FirstOrDefaultAsync();\n\n                if (entity is null)\n                    return NotFound(nameof(NotFound));\n\n                var temp = await databaseContext.Table1.RemoveAsync(entity);\n\n                return Ok(temp);\n            }\n\n            [HttpGet(\"TestQueryString\")]\n            public async Task\u003cIActionResult\u003e TestQueryString()\n            {\n\n                var query = databaseContext.Table1;\n\n                var result = query\n                    .Take(3)\n                    .Skip(2)\n                    .Where(\"name = 'TestName'\")\n                    .OrderBy(p =\u003e p.name)\n                    .OrderBy(p =\u003e p.is_archive)\n                    .OrderByDescending(p =\u003e p.stream_id)\n                    .OrderBy(p =\u003e p.creation_time)\n                    .Select(p =\u003e new FileEntityDto()\n                    {\n                        Name = p.name,\n                        Size = p.cached_file_size,\n                        Id = p.stream_id,\n                        Type = p.file_type\n                    });\n\n                return Ok(new\n                {\n                    Query = result.ToQueryString(),\n                    Data = await result.ToListAsync(p =\u003e new FileEntityDto()\n                    {\n                        Name = p.name,\n                        Size = p.cached_file_size,\n                        Id = p.stream_id,\n                        Type = p.file_type\n                    })\n                });\n            }\n\n        }\n\n        public class FileEntityDto\n        {\n            public Guid Id { get; set; }\n            public string? Name { get; set; }\n            public string? Type { get; set; }\n            public long Size { get; set; }\n        }\n    }\n    ```\n\n# Conclusion\n\nIn this article, we delved into creating a file management application using ASP.NET Core and SQL Server FileTable. This application provides functionalities for organizing and managing files in a web environment. Leveraging modern technologies and tools like FileTable, we were able to build a secure, reliable, and high-performance application.\n\n\n# Support\nIf you are having problems, please let me know by [raising a new issue](https://github.com/samanazadi1996/Sam.FileTableSqlServer/issues).\n\n# License\nThis project is licensed with the [MIT license](https://github.com/samanazadi1996/Sam.FileTableSqlServer?tab=MIT-1-ov-file#readme).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamanazadi1996%2Fsam.filetablesqlserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamanazadi1996%2Fsam.filetablesqlserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamanazadi1996%2Fsam.filetablesqlserver/lists"}