In this article I will explain with an example, how to upload (insert) file into SQL Server Database using
Entity Framework in ASP.Net Core Razor.
Database
This article makes use of table named tblFiles whose schema is defined as follows.
Note: You can download the database table SQL by clicking the download link below.
Model
The following Model class consists of four properties.
public class FileModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string ContentType { get; set; }
public byte[] Data { get; set; }
}
Database Context
Once the
Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
using Microsoft.EntityFrameworkCore;
namespace Insert_File_Database_EF_Razor_Core
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<FileModel> tblFiles { get; set; }
}
}
Namespaces
You will need to import the following namespaces.
using System.IO;
using Microsoft.AspNetCore.Http;
Razor PageModel (Code-Behind)
The PageModel consists of the following two Handler methods.
Handler method for handling GET operation
This Handler method is left empty as it is not required.
Handler method for handling File Upload operation
This Handler method gets called, when a File is selected and the Upload Button is clicked and it accepts an object of IFormFile class as a parameter.
The file posted from the Form inside the Razor page is received through this parameter.
Note: The name of the IFormFile parameter and the name of HTML FileUpload element must be exact same, otherwise the IFormFile parameter will be NULL.
Then, the uploaded file is converted to an Array of Bytes using
MemoryStream and finally, inserted into the database table using
Entity Framework.
public class IndexModel : PageModel
{
private DBCtx Context { get; }
public IndexModel(DBCtx _context)
{
this.Context = _context;
}
public void OnGet()
{
}
public void OnPostUpload(IFormFile postedFile)
{
string fileName = Path.GetFileName(postedFile.FileName);
string contentType = postedFile.ContentType;
using (MemoryStream ms = new MemoryStream())
{
postedFile.CopyTo(ms);
this.Context.tblFiles.Add(new FileModel
{
Name = fileName,
ContentType = contentType,
Data = ms.ToArray()
});
this.Context.SaveChanges();
}
}
}
Razor Page (HTML)
The HTML of Razor Page consists of an HTML Form consisting of an HTML FileUpload element, a Submit Button control.
The HTML Form has been specified with enctype=“multipart/form-data” attribute as it is necessary for File Upload operation.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostUpload, but here it will be specified as Upload when calling from the Razor HTML Page.
Form Submit
When the Upload Button is clicked, the form is submitted to the Handler method.
@page
@model Insert_File_Database_EF_Razor_Core.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<span>Select File:</span>
<input type="file" name="postedFile" />
<input type="submit" value="Upload" asp-page-handler="Upload" />
</form>
</body>
</html>
Screenshots
The inserted files in Database table
Downloads