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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:43:50+00:00 2026-05-20T16:43:50+00:00

I am using Entity Framework 4.0 POCO with WPF. I have a form displaying

  • 0

I am using Entity Framework 4.0 POCO with WPF.
I have a form displaying to the user a an object with a many to many relationship like the following:

public class BPartner : BaseEntity
{
    public string Name 
    { 
        get { return name; } 
        set 
        { 
            if (name != value) 
            {
                name = bpartnerValidatorLight.ValidateName(value); 
                OnPropertyChanged("Name",true); 
            } 
        } 
    }
    public virtual ObservableCollection<BPAddress> BPAddresses { get; set; }

}

public class BPAddress : BaseEntity
{
    public string Line1 
    { 
        get 
        { 
            return line1; 
        } 
        set 
        { 
            if (line1 != value) 
            {
                line1 = bpAddressValidatorLight.ValidateLine1(value); 
                OnPropertyChanged("Line1",true); 
            } 
        } 
    }

    public virtual City City 
    { 
        get { return city; } 
        set 
        { 
            if (city != value) 
            {
                city = value; 
                OnPropertyChanged("City"); 
            } 
        } 
    }
}

The user may add and delete addresses in the BPAddresses collection and change the “Name” of the BPartner. When the user is done modifying the BPArtner object he/she may click “Save” or “Cancel”. The problem is that when the user clicks “Cancel” I need to tell the Entity Framework to revert all the changes.
Any approach for handling this is very welcome including reloading.
Here is what I have tried:
1. Discard the objectContext, create a new object context and just query the database to reload everything again. The problem here is that Entity Framework caches things and old objects linger attached to the old context and I get exceptions if the user clicks cancel then edits again and clicks save.
2.
repoBPartner.Refresh(bp);

       IQueryable<BPAddress> query = from e in addressRepo.AsQueryable()

                where e.BPartnerId == bp.Id
                select e;
        ObjectQuery<BPAddress> objectQuery = (ObjectQuery<BPAddress>)query;
        objectQuery.Include("City");
        objectQuery.Include("Country");
          ObjectResult<BPAddress> result =  (ObjectResult<BPAddress>)objectQuery.Execute(MergeOption.OverwriteChanges);
        bp.BPAddresses = new System.Collections.ObjectModel.ObservableCollection<BPAddress>(result.ToList<BPAddress>());

      The problem here is that "City" property does not get refreshed. 
 3. Tried: objectContext.LoadProperty(bp, "BPAddresses", MergeOption.OverwriteChanges);

All the above worked partially. What is the best practice for achieving this?

Any help will be appreciated.

Thanks,
K. Mitev

  • 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-05-20T16:43:51+00:00Added an answer on May 20, 2026 at 4:43 pm

    EF doesn’t offer discarding changes. EF also doesn’t have second level cache so once you dispose a context with changes and create a new context to load data from database you will get unchanged data.

    The problem here is that Entity
    Framework caches things and old
    objects linger attached to the old
    context and I get exceptions if the
    user clicks cancel then edits again
    and clicks save.

    When user clicks cancel you must throw away all objects used in editation together with their context. These objects and context are dead. Once user clicks edit again you must load everything again from database.

    Refreshing navigation properties doesn’t work very well. For example once you add something to navigation collection it will never be removed by refreshing from database.

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

Sidebar

Related Questions

I am using Entity Framework and I have two tables, a contact relationship table
Using Entity Framework 4.1 code first and POCO entities in MVC3; I would like
I have been using the Entity Framework with the POCO First approach. I have
I am using Entity Framework 4 with the POCO code generator. I have a
I have an assembly generated using POCO template using Entity Framework (e.g. Company.Models.dll).Besides generated
I am using Entity Framework 4 with the Service/Repository/EF4/POCO classes technique, and I have
Using Entity Framework 4.1 I have the following tables I'm using with Entity Framework
I have combined RIA Services with Entity Framework using POCO's. It's all worked wonderfully
My team is using Entity Framework 4.3.0 - Code Only with POCO classes as
I am using Entity Framework, POCO entities and repository pattern for data access. I

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.