Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:26:05+00:00 2026-06-11T22:26:05+00:00

An image has many galleries, and a gallery has many images. I am trying

  • 0

An image has many galleries, and a gallery has many images. I am trying to update the images that a gallery has. The code below works, but feels somewhat clumsy.

   var g=db.Galleries.Find(gal.Id);

   var ims = gal.Images.Select(i => db.Images.Where(im => im.Id == i.Id && im.User.Id == user.Id)).SelectMany(im => im).ToList();

   g.Name = gal.Name;

   g.Images.Clear();

   foreach (var im in ims)
   {
        g.Images.Add(im);
   }

   db.SaveChanges();

When I do this:

g.Images=ims;

instead of:

   g.Images.Clear();

   foreach (var im in ims)
   {
        g.Images.Add(im);
   }

An exception is thrown:

Violation of PRIMARY KEY constraint 'PK_GalleryImages'. Cannot insert duplicate key in object 'dbo.GalleryImages'.

Can you explain why? Is there a better way to approach this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:26:07+00:00Added an answer on June 11, 2026 at 10:26 pm

    When you do g.Images.Add(im), entity framework will add a new row with galleryId and imageId in dbo.GalleryImages table if it doesn’t already exist in the table.

    On the otherhand, when you do g.Images=ims, you are actually telling entity framework to add a new relation (new row in GalleryImages table). If you were to add new images into the gallery g.Images=ims would work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've an image gallery, that displays images in a 5X5 matrix. Each image has
I have these two tables: Galleries - {id,title,description} Images - {id,gallery_id,file} Each gallery has
I'm working on a website that has many tiled images using JQuery's Masonry plugin.
A Brand has an Image (logo - not nullable). A Product has many Images.
I'm trying to write an ActiveAdmin form for my Gallery model that has a
Update to prevent any more wrong answers: A product has many product images There
I have an image that will update many times a second. I want to
I have a grid of many images and each image has a CSS class
My div has many images like <div id=gallery> <img src=images/flowing-rock.jpg alt=Flowing Rock width=580 height=360
my Page has many hover images, sometimes there are backgrounds from div-elements or src

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.