I try to (multi) upload file to database, and at the same time save the file attributes to sql database.
The problem is, saving file attributes to database when I try to do multiple upload (Single file upload return no error).
In the process, the first file attributes successfully saved to the database, but the second one failed, with the error message :
A database operation failed while processing the request.
SqlException: SqlException: Cannot insert explicit value for identity column
in table 'TestClass' when IDENTITY_INSERT is set to OFF.
There are pending model changes
Pending model changes are detected in the following:
ApplicationDbContext
In Visual Studio, use the Package Manager Console to scaffold a new migration
for these changes and apply them to the database:
PM> Add-Migration [migration name]
PM> Update-Database
Alternatively, you can scaffold a new migration and apply it from a command prompt
at your project directory:
> dotnet ef migrations add [migration name]
> dotnet ef database update
I also did scaffold a new migration as suggested above, but to no avail.
Here is the Model.
public class TestClass
{
[Key]
public int Id { get; set; }
public string FileName { get; set; }
public string ApplicationUserId { get; set; }
}
Here is the View.
<input id="postedFiles" type="file" runat="server" name="postedFiles" multiple>
<input type="submit" value="upload" class="btn-sm btn-primary" />
And here is the controller. I removed the physical file upload part to focus only on the problem.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Uploadpic(List<IFormFile> postedFiles, string? id, TestClass UpData)
{
if (id == null)
{
return BadRequest();
}
foreach (IFormFile postedFile in postedFiles)
{
UpData.ApplicationUserId = id;
UpData.FileName = postedFile.FileName;
await _db.TestClasses.AddAsync(UpData);
await _db.SaveChangesAsync();
}
return View();
}