Current version of EntityFramework (RC1-final) has no DbSet.Local feature. But! You can achieve something similar with current extension method:
public static class Extensions
{
public static ObservableCollection<TEntity> GetLocal<TEntity>(this DbSet<TEntity> set)
where TEntity : class
{
var context = set.GetService<DbContext>();
var data = context.ChangeTracker.Entries<TEntity>().Select(e => e.Entity);
var collection = new ObservableCollection<TEntity>(data);
collection.CollectionChanged += (s, e) =>
{
if (e.NewItems != null)
{
context.AddRange(e.NewItems.Cast<TEntity>());
}
if (e.OldItems != null)
{
context.RemoveRange(e.OldItems.Cast<TEntity>());
}
};
return collection;
}
}
Note: it won't refresh the list if you query for more data. It will sync changes to the list back into the change tracker though.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…