简体   繁体   English

从 ASP.NET 内核上传多个文件并存储到数据库中

[英]Upload multiple files and store into a database from ASP.NET Core

I have this method to upload files & insert folder name into database.我有这种方法可以上传文件并将文件夹名称插入数据库。 The upload into the folder is working, but the insert into the database fails - I get this error上传到文件夹工作正常,但插入数据库失败 - 我收到此错误

Cannot insert explicit value for identity column in table 'Image' when IDENTITY_INSERT is set to OFF当 IDENTITY_INSERT 设置为 OFF 时,无法在表“图像”中插入标识列的显式值

Code:代码:

if (files != null)
{
    foreach (var file in files)
    {
        if (file.Length > 0)
        {
            //Getting FileName
            var fileName = Path.GetFileName(file.FileName);

            //Assigning Unique Filename (Guid)
            var myUniqueFileName = Convert.ToString(Guid.NewGuid());

            //Getting file Extension
            var fileExtension = Path.GetExtension(fileName);

            // concatenating  FileName + FileExtension
            var newFileName = String.Concat(myUniqueFileName, fileExtension);

            // Combines two strings into a path.
            var filepath =  new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Image")).Root + $@"\{newFileName}";

            using (FileStream fs = System.IO.File.Create(filepath))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            image.file= fileName;
            _context.Add(image);
           await _context.SaveChangesAsync();
        }
    }
}

My EF class Image :我的 EF class Image

public partial class Image
{
    public int ImageId { get; set; }
    public string ImageName { get; set; }
    public string ImageContentType { get; set; }
    public byte[] ImageData { get; set; }
    public bool? IsDelete { get; set; }
    public bool? ImageCover { get; set; }
}

Add the following to your entity:将以下内容添加到您的实体:

public partial class Image
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ImageId { get; set; }
    public string ImageName { get; set; }
    public string ImageContentType { get; set; }
    public byte[] ImageData { get; set; }
    public bool? IsDelete { get; set; }
    public bool? ImageCover { get; set; }
}

There is also the following option:还有以下选项:

_context.Database.OpenConnection();
try
{
    _context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Images ON");
    await _context.SaveChangesAsync();
    _context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Images OFF");
}
finally
{
    _context.Database.CloseConnection();
}

Source: https://entityframeworkcore.com/saving-data-identity-insert#:~:text=When%20you%20have%20a%20primary,before%20calling%20SaveChanges()%20manually .来源: https://entityframeworkcore.com/saving-data-identity-insert#:~:text=When%20you%20have%20a%20primary,before%20calling%20SaveChanges()%20manually

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM