通用存储库插入多个记录

时间:2017-10-04 11:46:01

标签: c# asp.net-mvc .net-core

我已经管理了一个通用存储库。当我在表格中插入一条记录时,它可以工作。如果我想上传许多图像并将其路径保存到数据库,这意味着我可以有1到n个记录,而不是问题。

我收到错误

  

SqlException:无法在标识列中插入显式值   表' ItemImages'当IDENTITY_INSERT设置为OFF时。

只保存第一条记录,其他n条记录不会保存到数据库中。

这是存储库类:

public class Repository<T> : IRepository<T> where T : BaseEntity
    {
        private ApplicationDbContext _context;
        private DbSet<T> entities;

    public Repository(ApplicationDbContext context)
    {
        _context = context;
        entities = _context.Set<T>();
    }
    public void Delete(T entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("Entity");
        }
        entities.Remove(entity);
    }

    public void Dispose()
    {
        _context.Dispose();
    }

    public T Get(int id)
    {
        return entities.SingleOrDefault(s => s.Id == id);
    }

    public T Get(Expression<Func<T, bool>> predicate)
    {
        return entities.Where(predicate).SingleOrDefault();
    }

    public T Get(Expression<Func<T, bool>> predicate, string[] includes)
    {
        return includes
                .Aggregate(entities.AsQueryable(), (query, path) => query.Include(path))
                .Where(predicate)
                .SingleOrDefault();
    }

    public IEnumerable<T> GetAll()
    {
        return entities.ToList();
    }

    public IEnumerable<T> GetAll(string[] includes)
    {
        return includes.Aggregate(entities.AsQueryable(), (query, path) => query.Include(path));
    }

    public IEnumerable<T> GetAll(Expression<Func<T, bool>> predicate)
    {
        return entities.Where(predicate).ToList();
    }

    public IEnumerable<T> GetAll(Expression<Func<T, bool>> predicate, string[] includes)
    {
        return includes
                .Aggregate(entities.AsQueryable(), (query, path) => query.Include(path))
                .Where(predicate)
                .ToList();
    }

    public void Insert(T entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("Entity");
        }
        entities.Add(entity);
    }

    public async Task<bool> SaveChangesAsync()
    {

        return (await _context.SaveChangesAsync() > 0);

    }

    public void Update(T entity)
    {

        entities.Update(entity);
    }

以下是我要插入记录的代码:

        [HttpPost]
        public async Task<IActionResult> Create([FromForm] ItemImageViewModel viewModel, List<IFormFile> files)
        {
            if (ModelState.IsValid)
            {
                var newItemImage = Mapper.Map<ItemImage>(viewModel);

                foreach (var file in files)
                {
                    var filePath = Path.Combine(_hostingEnviroment.WebRootPath + "\\images", file.FileName);
                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {

                        await file.CopyToAsync(fileStream);
                        newItemImage.Name = "\\images\\" + file.FileName;
                        newItemImage.CreateDate = DateTime.Now;
                    }
                    _repository.Insert(newItemImage);
                    await _repository.SaveChangesAsync();
                }

            }
            return RedirectToAction("Index");
        }

ItemImage类

public class ItemImage : BaseEntity
    {
        [ForeignKey("ItemId")]
        public virtual Item Item { get; set; }

        public int ItemId { get; set; }
    }

    public abstract class BaseEntity
    {
        [Key]
        public int Id { get; set; }

        public DateTime CreateDate { get; set; }


        public DateTime? UpdateDate { get; set; }

        [Required]
        [StringLength(255)]
        public string Name { get; set; }

        [StringLength(4096)]
        public string Description { get; set; }

        public string UserId { get; set; }

        [ForeignKey("UserId")]
        public virtual ApplicationUser ApplicationUser { get; set; }
    }

0 个答案:

没有答案