-1

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();
    }
Waller
  • 433
  • 1
  • 6
  • 20
  • https://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity - your exact error message – pm100 Mar 30 '22 at 03:17
  • 1
    Does this answer your question? [Cannot insert explicit value for identity column in table 'table' when IDENTITY\_INSERT is set to OFF](https://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity) – pm100 Mar 30 '22 at 03:17

1 Answers1

0

I figure out the answer from this post. What I did was I create an new object in every loop. In the controller,

    foreach (IFormFile postedFile in postedFiles)
    {
                var UpData = new TestClass(); //HERE,CREATE NEW OBJECT EACH TIME
                UpData.ApplicationUserId = id;
                UpData.FileName = postedFile.FileName;
                await _db.TestClasses.AddAsync(UpData);
                await _db.SaveChangesAsync();         
    }
Waller
  • 433
  • 1
  • 6
  • 20