I have 2 objects with one to many relation.
I'm trying to add multiple ProductImages with the same ProductId to the data base, while using a generic repository.

I have tried saving a list of ProductImage to a repository with dbSet.AddRange . after that i saved it with context.SaveChanges();
i have also tried looping trough the list of ProductImage and adding each item with dbSet.Add .and than context.SaveChanges() .

repository methods:

```
    public virtual void Add(TEntity entity)
        {
            dbSet.Add(entity);
        }

        public virtual void AddRange(List<TEntity> entityList)
        {
            dbSet.AddRange(entityList);
        }
        public void Save()
        {
            context.SaveChanges();
    }

```

the result is the object saved to the data base is always the last one - but i want to save all of them.

**Edit**
the method for adding the Objects:

```
        private  Task<int> InsertProductImagesToDB(List<ProductImageVM> productImages, int productId, List<IFormFile> imageFiles)
        {
            if (productImages?.Any()==true)
            {
                List<ProductImage> dbEntitys = new List<ProductImage>();
                for(int i=0; i< productImages.Count; i++)
                {
                    ProductImage p = productImages[i].ConvertToProductImageEntity(imageFiles[i], productId);
                    dbEntitys.Add(p);
                }
                ImagesRepository.AddRange(dbEntitys);
                ImagesRepository.Save();
                return Task.FromResult(dbEntitys.First().Id);//return first productImage as the main image
            }
            return Task.FromResult(-1);
        }

```

as some of you mentioned in the comments - the object constructor is not outside the loop.
while debugging i can see the dbEntitys list contains 2 items with defferent values.

image of the dbset after adding the objects before saving changes:
[![enter image description here](https://i.stack.imgur.com/o4L3l.jpg)](https://i.stack.imgur.com/o4L3l.jpg)
db table after changes were saved:
[![enter image description here](https://i.stack.imgur.com/vWDBu.jpg)](https://i.stack.imgur.com/vWDBu.jpg)