質問

We have a IRepository pattern working quite well for us with our EF and POCO first setup. However we are getting a strange problem with a "Collection was modified; enumeration operation may not execute".

Basically we have a repository with an ObjectSet as follows:

protected IObjectSet<T> ObjectSet
{
  get
  {
    if (_objectSet == null)
    {
      _objectSet = this.Context.CreateObjectSet<T>();
    }

    return _objectSet;
  }
}

And a delete method on the IRepository class which is implemented as follows:

public void Delete(T entity)
{
  ObjectSet.DeleteObject(entity);
}

It's all very straightforward and we've had no problems up until this point but whenever we try to delete an object of a collection we get this error. If I put a breakpoint on the delete method hover over ObjectSet and expand the results so it's all loaded then the DeleteObject works fine but if all the items are not loaded form the ObjectSet it fails with the collection modified error.

I set up a testbed not using POCO or IRepository and it works fine so basically is there something really obbvious that I am missing. We are quite deep into using this pattern in multiple projects and people have been working around it rather than trying to fix it which isn't an option I don't think.

Many thanks for any help or insight.

EDIT:

This is very strange but when I remove certain entities from the model this error goes away but we have a huge model and I can't track it down as I first removed everything but the specific tables in the delete. Has anyone ever come across this before, we are using inheritance tables (per type) but I can't prove this is the problem.

SECOND EDIT:

OK I removed everything from the model except the table per type inheritance entites and the error occurs, when I remove all but one derived type and try to delete from that it works fine. So my question is has anyone else had this problem when using the table per type inheritance?

役に立ちましたか?

解決

It's a bug in Entity Framework. Fixed in 4.5 according to connect.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top