是否有一个DbSet< TEntity>。当地等同于实体框架7? [英] Is there a DbSet<TEntity>.Local equivalent in Entity Framework 7?
本文介绍了是否有一个DbSet< TEntity>。当地等同于实体框架7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个
ObservableCollection<TEntity>
在EF7,
DbSet<TEntity>.Local
似乎不存在;
有没有什么解决方法吗?
Is there any workaround?
推荐答案
的EntityFramework的当前版本(RC1-最后的)没有DbSet.Local功能。但!可以实现与现有的扩展方法类似:
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.
这篇关于是否有一个DbSet< TEntity>。当地等同于实体框架7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文