OVERVIEW/DESCRIPTION
简单:从添加到的TEntity中删除运行时类型的多态对象不会引发ObjectSet<TEntity>.IListSource.GetList()方法返回的IBindingList对象上的IBindingList.ListChanged事件。
但是,删除其运行时类型匹配 TEntity的实例将有效地在ListChanged事件上得到通知。
为了澄清,在任何时候都有效地从底层集合或数据视图/存储中移除对象,但是当这些对象是严格从实际使用的TEntity派生的类型的实例时,不会引发ListChanged事件来通知它们的删除。
对于对集合的运行时多态性的适当数据绑定支持而言,这只是一个显着的错误。
复制
模型设置
根据Server2012Express. Strategy.
这是实体层次结构(伪UML):
FiascoEntityContext : ObjectContext
+ Foos : ObjectSet<Foo>
Foo : EntityObject
+ Id: Int32
+ Name: String
SpecialFoo : Foo
+ SpecialProperty: String演示代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data.Objects;
namespace FiascoEF {
class Program {
static void Main(string[] args) {
using (FiascoEntityContext context = new FiascoEntityContext()) {
//
// add some foos
//
context.Foos.AddObject(new Foo { Name = "Foo1" });
context.Foos.AddObject(new BetterFoo { Name = "BetterFoo1", SpecialProperty = "Something Special" });
context.SaveChanges();
//
// show the contents
//
Console.WriteLine("Listing all foos:");
foreach (var foo in context.Foos) {
Console.WriteLine(" Got {0}: Id={1} Name={2} (State={3})", foo, foo.Id, foo.Name, foo.EntityState);
}
//
// attach handler for the ListChanged event of the IBindingList returned by context.Foos as IListSource
// NOTE: have to do this here bacause SaveChanges() above will reset the internal IBindingList
//
var bindingList = (context.Foos as IListSource).GetList() as IBindingList;
bindingList.ListChanged += new ListChangedEventHandler(bindingList_ListChanged);
//
// delete all foos and show state. expect the event handler above to be invoked.
//
Console.WriteLine("Deleting all foos:");
foreach (var foo in context.Foos) {
context.Foos.DeleteObject(foo);
Console.WriteLine(" Deleted {0}: Id={1} Name={2} (State={3})", foo, foo.Id, foo.Name, foo.EntityState);
}
context.SaveChanges();
}
}
static void bindingList_ListChanged(object sender, ListChangedEventArgs e) {
Console.WriteLine(" Event on {0}: {1}", sender, e.ListChangedType);
}
}
}预期结果
Listing all foos:
Got FiascoEF.Foo: Id=257 Name=Foo1 (State=Unchanged)
Got FiascoEF.BetterFoo: Id=258 Name=BetterFoo1 (State=Unchanged)
Deleting all foos:
Event on System.Data.Objects.ObjectView`1[FiascoEF.Foo]: ItemDeleted
Deleted FiascoEF.Foo: Id=257 Name=Foo1 (State=Deleted)
Event on System.Data.Objects.ObjectView`1[FiascoEF.Foo]: ItemDeleted
Deleted FiascoEF.BetterFoo: Id=258 Name=BetterFoo1 (State=Deleted)实际结果
Listing all foos:
Got FiascoEF.Foo: Id=257 Name=Foo1 (State=Unchanged)
Got FiascoEF.BetterFoo: Id=258 Name=BetterFoo1 (State=Unchanged)
Deleting all foos:
Event on System.Data.Objects.ObjectView`1[FiascoEF.Foo]: ItemDeleted
Deleted FiascoEF.Foo: Id=257 Name=Foo1 (State=Deleted)
Deleted FiascoEF.BetterFoo: Id=258 Name=BetterFoo1 (State=Deleted)研究
通过反射器发现返回的实际IBindingList为ObjectView<TElement>类型,此类型将删除操作委托给内部IObjectViewData<TElement>。该接口的实现是ObjectViewQueryResultData<TElement>,它定义:
public ListChangedEventArgs OnCollectionChanged(object sender, CollectionChangeEventArgs e, ObjectViewListener listener) {
ListChangedEventArgs changeArgs = null;
if (e.Element.GetType().IsAssignableFrom(typeof(TElement)) && _bindingList.Contains((TElement) (e.Element))) {
...
changeArgs = new ListChangedEventArgs(ListChangedType.ItemDeleted, ...);
...
}
return changeArgs;
}支票:
if (e.Element.GetType().IsAssignableFrom(typeof(TElement)) && ...) { ... }似乎是假的..。以下可能是有意做的?
if (typeof(TElement).IsAssignableFrom(e.Element.GetType()) && ...) { ... }发布于 2012-07-19 15:15:06
很公平,错误报告给微软-http://connect.microsoft.com/VisualStudio/feedback/details/749368.他们似乎已经承认了这个问题,但还不清楚他们将做什么。
请记住,我们讨论的是IBindingList实现,当作为数据绑定的目的被视为IListSource时,ObjectSet<T>检索到了该实现,因此事件将被触发,就像在同构列表的情况下一样。
我定义了一个从ObservableCollection<T>继承并包装ObjectSet<T>的类,然后借助DbExtensions.ToBindingList<T>(this ObservableCollection<T>)扩展方法实现了IListSource。
或者,我可以继续使用DbContext API,但是定义自己的ObservableCollection<T>允许我现在继续使用ObjectContext,这正是我想要的。
https://stackoverflow.com/questions/11061548
复制相似问题