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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:27:54+00:00 2026-06-15T17:27:54+00:00

I think I am missing something simple here. I am getting the error: Violation

  • 0

I think I am missing something simple here. I am getting the error:

“Violation of PRIMARY KEY constraint ‘PK_FeatureTypes’. Cannot insert duplicate key in object ‘dbo.FeatureTypeCodes’. The duplicate key value is (28).\r\nThe statement has been terminated”

I have a look-up / linked table of FeatureType – (Mountain, Lake, River, etc.) which is already populated with data and is defined as:

[Table("FeatureTypeCodes")]
public class FeatureTypeCode {
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int FeatureTypeCodeID { get; set; }
    public string Name { get; set; }
}

This is linked to my place table / object like this:

[Table("Places")]
public class Place {
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int PlaceID { get; set; }
    public string Name { get; set; }
    public FeatureTypeCode FeatureTypeCode { get; set; }
    public ICollection<PlaceCoordinate> PlaceCoordinates { get; set; }
}

Then I am loading them from the old database like this (it is part of my conversion code):

foreach (DataRow r in table.Rows) {
        int ftID = Convert.ToInt32(r["FeatureTypeId"]);
        Place temp = new Place {
                PlaceID = Convert.ToInt32(r["PlaceID"]),
                Name = r["PlaceName"].ToString(),
                FeatureTypeCode = featureTypeCodeRepository.FeatureTypeCodes.FirstOrDefault(o=>o.FeatureTypeCodeID == ftID)
            };
            places.Add(temp);
        }

The error is being generated when it tries to insert a new FeatureType object with the same ID as an existing object while saving a Place. My thought was that by loading FeatureType from the context it would not attempt to insert a new FeatureType on saving the Place object. I am obviously wrong on that, but is it something simple I am missing?

  • 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-15T17:27:54+00:00Added an answer on June 15, 2026 at 5:27 pm

    I don’t think that you use the same DBContext Object in your featureTypeCodeRepository and the places.Add(temp);. So I think that basically EF don’t keep track of the FeatureTypeCodes becuse it’s loaded by one context, and saved by another.

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

Sidebar

Related Questions

I think I'm missing something simple here... $key = deco-1-LB-700F:MAR:40; if ($key =~ m/deco-(.*?)-(.*?)-(.*?):(.*?):(.*?)/)
I think i must be missing something simple here but I can't figure out
I think I'm just missing something simple here but here is what I am
I think I'm missing something simple here. I'm using Boost's shared_ptr . shared_ptr<Foo> pA(new
I think I'm missing something very simple here. I have an EF4 ObjectContext that
I think that I'm missing something very simple here. I want to pass a
I think I'm missing something relatively simple here, but I'm trying to find a
I think I'm missing something trivial here. I derived simple control directly from Control
I think I'm missing something really obvious here... must be... but for some reason
I'm new to c# and I'm sure I'm missing something simple here. I'm trying

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.