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 MVC.
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_MVC_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;
Controller
The Controller consists of following two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
This Action 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 View is received through this parameter.
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 HomeController : Controller
{
private DBCtx Context { get; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(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();
}
return View();
}
}
View
Inside the View, in the very first line the FileModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the following TagHelpers attributes.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The HTML Form has been specified with enctype=“multipart/form-data” attribute as it is necessary for File Upload operation.
There is an HTML FileUpload element and a Submit Button enclosed in the Form element.
Form Submit
When the Upload Button is clicked the form is submitted to the Controllers Action Method.
@model Insert_File_Database_EF_MVC_Core.Models.FileModel
@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" asp-controller="Home" asp-action="Index" enctype="multipart/form-data">
<span>Select File:</span>
<input type="file" name="postedFile" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
Screenshots
The inserted files in Database table
Downloads