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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:48:48+00:00 2026-06-03T12:48:48+00:00

I am getting an error when calling entities.savechanges() on my EF 4.3.1. My database

  • 0

I am getting an error when calling entities.savechanges() on my EF 4.3.1. My database is a sql ce v4 store and I am coding in the mvvm pattern. I have a local version of my context that I send to an observable collection and modify etc. This works fine, and when I call savechanges() when no rows exist in the database the objects persist fine. When I reload the application, the objects are populated in my listbox as they should, however if I add another object and call savechanges() I get an error saying that a duplicate value cannot be inserted into a unique index.

From my understanding it means that the context is trying to save my entities to the datastore, but it seems to be adding my untouched original objects as well as the new one. I thought it would leave them alone, since their state is unchanged.

private void Load()
    {
        entities.Properties.Include("Images").Load();
        PropertyList = new ObservableCollection<Property>();
        PropertyList = entities.Properties.Local;        

        //Sort the list (based on previous session stored in database)
        var sortList = PropertyList.OrderBy(x => x.Sort).ToList();
        PropertyList.Clear();
        sortList.ForEach(PropertyList.Add);

        propertyView = CollectionViewSource.GetDefaultView(PropertyList);
        if (propertyView != null) propertyView.CurrentChanged += new System.EventHandler(propertyView_CurrentChanged);     


        private void NewProperty()
    {
        try
        {
            if (PropertyList != null)
            {                                             
                Property p = new Property()
                    {
                        ID = Guid.NewGuid(),
                        AgentName = "Firstname Lastname",
                        Address = "00 Blank Street",
                        AuctioneerName = "Firstname Lastname",
                        SaleTitle = "Insert a sales title",
                        Price = 0,
                        NextBid = 0,
                        CurrentImage = null,
                        Status = "Auction Pending",
                        QuadVis = false,
                        StatVis = false, //Pause button visibility
                        Sort = PropertyList.Count + 1,                            
                    };

                PropertyList.Add(p);
                SaveProperties();
            }

        private void SaveProperties()
    {
        try
        {               
            foreach (var image in entities.Images.Local.ToList())
            {
                if (image.Property == null)
                    entities.Images.Remove(image);
            }                
        }

        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message);
        }

        entities.SaveChanges();
    }
  • 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-03T12:48:50+00:00Added an answer on June 3, 2026 at 12:48 pm

    Without commenting on all the code here this is the bit that’s causing the specific problem you bring up:

    //Sort the list (based on previous session stored in database) 
    var sortList = PropertyList.OrderBy(x => x.Sort).ToList(); 
    PropertyList.Clear(); 
    sortList.ForEach(PropertyList.Add); 
    

    This code:

    • Starts with entities that have been queried and are being tracked by the context as Unchanged entities. That is, entities that are known to already exist in the database.
    • Creates a new sorted list of these entities.
    • Calls Clear on the local collection causing each tracked entity to be marked as deleted and removed from the collection.
    • Adds each entity back to the context putting it now in an Added state meaning that it is new and will be saved to the database when SaveChanges is called,

    So effectively you have told EF that all the entities that exist in the database actually don’t exist and need to be saved. So it tries to do this and it results in the exception you see.

    To fix this don’t clear the DbContext local collection and add entities back. Instead you should sort in the view using the local collection to back the view.

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

Sidebar

Related Questions

Getting error while inserting values into database (SQL Server 2008) Implicit conversion from data
I am getting following error: error: calling a host function(strcpy) from a __device__/__global__ function(doDecompression)
I am getting this error when calling a web service method which writes to
We are getting the error below calling c:\windows\syswow64\regsvr32.exe on Windows Server 2008 R2 x64.
Am getting following error message on calling WCF service: The formatter threw an exception
I'm getting the following error when calling a stored procedure: Cannot find the object
Getting a VerifyError using the TweenMax lib when calling TweenMax.to . This error doesn't
I am doing an MxRecordLookup. I am getting an error when calling the DnsRecordListFree
I'm getting this error when calling a java web service. I'm using the same
I am getting this error while calling $.ajax methods Error: [Exception... Access to restricted

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.