[英]Is there a DbSet<TEntity>.Local equivalent in Entity Framework 7?
I need an 我需要一个
ObservableCollection<TEntity>
in EF7, 在EF7中,
DbSet<TEntity>.Local
doesn't seem to exist; 似乎不存在;
Is there any workaround? 有没有解决方法?
Current version of EntityFramework (RC1-final) has no DbSet.Local feature. 当前版本的EntityFramework(RC1-final)没有DbSet.Local功能。 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. 它会将列表中的更改同步回更改跟踪器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.