我们希望观察所有的实体框架ChangeTracker
记录,它们是Modified
,Added
或Deleted
。
如何获取ChangeTracker
并将其转换为Observable
?希望在“帐户条目”表更改时进行订阅。
var entities = storeDbContext.ChangeTracker
.Entries()
.Where(x => x.State == EntityState.Modified || x.State == EntityState.Deleted || x.State == EntityState.Added)
source.Subscribe(x => Console.WriteLine(x).Where(x => x.Entity is AccountEntry));
资源:
Entity Framework ChangeTracker Stream and Save to Query Afterwards
您需要订阅/取消订阅事件的可观察对象:
Observable
.FromEventPattern<EntityStateChangedEventArgs>(
h => storeDbContext.ChangeTracker.StateChanged += h,
h => storeDbContext.ChangeTracker.StateChanged -= h)
.Where(x => x.NewState == EntityState.Modified || x.NewState == EntityState.Deleted || x.NewState == EntityState.Added)