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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With