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

  • SEARCH
  • Home
  • 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 9036379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:58:44+00:00 2026-06-16T08:58:44+00:00

I currently have an object like this (simplified): public class Image { public int

  • 0

I currently have an object like this (simplified):

public class Image {
    public int Id { get; set; }

    public int ExternalId { get; set; }
}

Now let’s say I have this method (mostly pseudo-code):

public void GetImage(int externalId) {
    var existingImage = db.Images.FirstOrDefault(i => i.ExternalId == externalId);

    if (existingImage != null) {
        return existingImage;
    }

    var newImage = new Image() { ExternalId = externalId };
    db.Images.Attach(newImage);
    db.SaveChanges();

    return newImage;
}

Because ExternalId isn’t a key, the change tracker won’t care if I have “duplicate” images in the tracker.

So now, let’s say this method gets called twice, at the same time via AJAX and Web API (my current scenario). It’s async, so there are two threads calling this method now.

If the time between calls is short enough (in my case it is), two rows will be added to the database with the same external ID because neither existing check will return a row. I’ve greatly simplified this example, since in my real one, there’s a timing issue as I fetch the “image” from a service.

How can I prevent this? I need the image to be returned regardless if it’s new or updated. I’ve added a Unique Constraint in the database, so I get an exception, but then on the client, the call fails whereas it should use the existing image instead of throwing an exception.

If I understand EF correctly, I could handle this by making ExternalId a primary key and then use concurrency to handle this, right? Is there any way to avoid changing my current model or is this the only option?

  • 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-16T08:58:45+00:00Added an answer on June 16, 2026 at 8:58 am

    If you already have property defining uniqueness of your entity (ExternalId) you should use it as a key instead of creating another dummy key which does not specify a real uniqueness of your entity. If you don’t use ExternalId as a key you must put unique constraint on that column in the database and handle exception in your code to load existing Image from the database.

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

Sidebar

Related Questions

Currently I have an object which I receive from a server, this contains two
I currently have a function: public static Attribute GetAttribute(MemberInfo Member, Type AttributeType) { Object[]
Currently I have a simple knockoutJS object, with a few observables. But this object
Currently I have this code var App = Ember.Application.create(); App.user = Ember.Object.create({ people: customers
Here is what I have currently, I'm doing this: current_account.send(object).search(params[:search]).user_id_equals_any(users).visibility_is_any(visibilities) but thats not very
This is a simplified version of a class that I have in my project.
Currently I have code like the following (simplified somewhat). Eventually, I've added more and
This is a simplified version of the original problem. I have a class called
I currently have a routes object that has several regular pages, and would like
I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar

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.