API to connect a FileTable in Blazor Wasm

sblb 1,171 Reputation points
2021-12-31T15:33:00.9+00:00

Hi,
I've created a Filetable via SqL server 2019 to have an architecture of the file (quantities 100 000 files).
I would now like to display my database file on a razor page to open the files directly from my interface. Is this possible? Do you have any tips and/or tutorials on how to interface with a database filetable?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,492 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,605 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,076 Reputation points Microsoft Vendor
    2022-01-05T05:48:14.973+00:00

    Hi @sblb ,

    Whether you are using Entity Framework Core (EF core) or Entity Framework to map the database table and the entity classes in the application?

    Generally, in Asp.net core application, we will use the EF core to map the database table and the entity classes. And use the Dependency Injection to register the DbContext and the Repository.

    When we configure/register the DbContext, we can set the connection string via the DbContextOptionsBuilder, like this:

    public class BloggingContext : DbContext  
    {  
        public DbSet<Blog> Blogs { get; set; }  
        public DbSet<Post> Posts { get; set; }  
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)  
        {  
            optionsBuilder.UseSqlServer(  
                @"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True");  
        }  
    }  
    

    Or register it in the Startup.cs (asp.net core 1 ~ 5) or Program.cs file (asp.net 6), like this:

            services.AddDbContext<SchoolContext>(options =>  
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));  
    

    More detail information, refer to Tutorial: Get started with EF Core in an ASP.NET MVC web app (in WebAPI application, it will use the similar steps) and Configuration in ASP.NET Core (To get the connection string stored in the appsetting.json file).

    Finally, you can refer this article and sample to use EF core with FileTable, and build a API application to query data from FileTable.

    ASP.NET Core 5 MVC file upload / download with MS SQL Server FileTable


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


12 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 60,871 Reputation points
    2021-12-31T16:23:32.46+00:00

    You will need to create a webapi project that exposes the file table and files to the blazor app. As a list of a 100k files is too large, be sure to implement paging and probably a search function. You will also need an api call to return the actual file.

    0 comments No comments

  2. AgaveJoe 27,581 Reputation points
    2021-12-31T16:34:58.533+00:00

    Regardless of how the files are stored, the actual file contents must be downloaded from a web server to the browser (client). Blazor WASM cannot directly access SQL Server. A web service like Web API is required to return the file contents to the Blazor application (browser).

    Do you have any tips and/or tutorials on how to interface with a database filetable?

    According to the documentation, a FileTable acts like the Windows file system. Use the standard System.IO namespace to interact with the FileTable.

    Access FileTables with File Input-Output APIs

    0 comments No comments

  3. sblb 1,171 Reputation points
    2022-01-01T17:38:57.607+00:00

    Hi, thanks to your reply.

    I've configured the data acces of the following way :

    using System.Data.Entity;
    using DataAccess.Model;
    
    namespace DataAccess
    {
        public class FileContext : DbContext
        {
            public DbSet<FileDescription> FileDescriptions { get; set; }
        }
    }
    

    I've created a model class for the table.

    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace DataAccess.Model
    {
        public class FileDescription
        {
            [Key]
            public int Id { get; set; }
            public string FileName { get; set; }
            public string Description { get; set; }
            public DateTime CreatedTimestamp { get; set; }
            public DateTime UpdatedTimestamp { get; set; }
            public string ContentType { get; set; }
            public string Name { get; set; }
        }
    }
    

    How can I configure the connection string ? the name of the data base is SQLFileTable
    Below, I tried to define the connection string but it's not ok.
    In my connection string I should find filecontext that I defined in DataAcess class

    "ConnectionStrings": {
        "DefaultConnection": "Data Source=.;Initial Catalog=SQLFileTable;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
      },
    
    0 comments No comments

  4. Bruce (SqlWork.com) 60,871 Reputation points
    2022-01-01T17:53:51.32+00:00

    Are you sure you are use a file table? You don’t get to define the schema, and yours does not match a file table schema

    https://learn.microsoft.com/en-us/sql/relational-databases/blob/filetable-schema?view=sql-server-ver15

    Also you show no webapi code that connects to the database. Typically you’d inject a dbcontext as a service in program.cs startup and use in api actions.

    0 comments No comments