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 8130325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:40:15+00:00 2026-06-06T08:40:15+00:00

I’ve got a web service that is returning objects for me, along with a

  • 0

I’ve got a web service that is returning objects for me, along with a candidate key, which I’ve marked up with:

[Column(IsPrimaryKey = true)]
public int EventId { get; set; }

All is fine with loading the data back from the webservice, but whilst iterating through the new & updated items to put them in a SQL CE database to act as a cache, like so:

foreach (var e in results)
{
    var ev = (from evt in context.Events where evt.EventId == e.EventId select evt).FirstOrDefault();
    if (ev == null)
    {
        // Brand new
        context.Events.InsertOnSubmit(e);
    }
    else
    {
        // Update data
        ...
    }
}

For some reason, it occasionally thinks an event is brand new but it throws an exception during the InsertOnSubmit:

System.InvalidOperationException was unhandled

Cannot add an entity that already exists.

I’ve pulled the database off of the emulator with the Windows Phone Power Tools, loaded the database up in Visual Studio, and there doesn’t seem to be any conflicting value for the primary key, so why am I getting an exception that implies there is a conflict, and the debugger shows there aren’t any cross-thread issues?

EDIT

One thing I did spot, is that my entity has an overridden Equals() that didn’t cover the primary key (it did a comparison on a natural key), and it appears the web service has two records on something that is documented as a candidate key.

If I adjust the Equals method to account for the primary key as well, the exception changes to be a SqlCeException and it tells me that:

A duplicate value cannot be inserted into a unique index. [ Table name = SpecialEvent,Constraint name = PK_SpecialEvent ]

Even though the primary key still hasn’t been duplicated, which leaves me more confused (especially, as that type of exception cannot be easily caught)

EDIT2

I’ve even tried using a lock() {} around the code performing the update, but I’m still getting odd conflicts, so I’m confused why I’m occasionally getting conflicts, especially when the SDF doesn’t reflect the same conflicts.

  • 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-06T08:40:17+00:00Added an answer on June 6, 2026 at 8:40 am

    In my case, it turns out that it was down to a couple of factors combining — my entities did not have a property with IsVersion = true nor did they have UpdateCheck = UpdateCheck.Never on columns that did not make up part of my primary key. It appears there was a timing issue which was resulting in it trying trying to update only when the columns matched what it was expecting the old values to be.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

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.