1

Closed

Revert change that checks if navigation properties are ICollection

description

In EF6 we switched a check that navigation properties are IEnumerable to one that checks if they are ICollection. This is because almost nothing works if the property is IEnumerable but not ICollection and the intention was to help people discover why their code would likely not work. However, as this thread indicates "almost nothing" is not the same as "nothing": https://entityframework.codeplex.com/discussions/483925#post1150336

Therefore, we should switch the check back.
Closed Jun 16, 2014 at 5:37 PM by RoMiller
EF Team Triage: Moving back to the fixed status since the newly reported exception is actually about interfaces rather than collection type. Per Arthur's comments, support for interfaces can be voted on here - http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1272291-support-interfaces-as-entity-and-complex-types.

comments

ajcvickers wrote Jan 9, 2014 at 9:12 PM

Fixed in c754d7ecae28: Never Mind the Collections, Here's the Nav Pistols (1977: Revert nav prop check back to IEnumerable)

mgirgin wrote Jan 10, 2014 at 7:18 PM

** Closed by mgirgin 01/10/2014 12:18PM

matt6502 wrote May 13, 2014 at 9:37 PM

I have installed EF6.1 using Nuget, but I am still seeing this issue.

udayarc wrote May 15, 2014 at 4:36 PM

It didnt work for either..the file version 6.1.30225.0 is throwing same error

ajcvickers wrote May 15, 2014 at 6:33 PM

@matt6502 or @udayarc Can one or both of you provide the full text of the exception message and a stack trace?

Note that in general EF6.x does not support navigation properties that are not ICollection<T>. However, there was one place in the code where we didn't check for this and instead checked for IEnumerable<T>. One person reported that changing this code to check for ICollection<T> broke code that was working with a previous version of EF. I have no idea how they had code working with a previous version of EF that was using IEnumerable<T> because that just doesn't work almost everywhere in the stack, but I am assuming that there are some simple code paths where EF will at least not throw. It is still the case that nav properties that are not ICollection<T> have never worked with EF and still do not work with EF 6.x.

cnboland wrote Jun 10, 2014 at 3:40 PM

I'm still getting the '... must implement ICollection<T> in order for Entity Framework to be able to track changes in collections.' error in 6.1.0 and 6.1.1-beta1. To be clear, the target of the exception is the RequestItems collection, as see below, and the exception is thrown when a new Request object is added to the DbContext.

interface IRequestItem { ... }

interface IRequest
{
...
ICollection<IRequestItem> RequestItems { get; set; }
}

class RequestItem : IRequestItem { ... }

class Request : IRequest
{
ICollection<IRequestItem> RequestItems { get; set; }
}


Here is the exception message and stack trace (from unit test execution):

Test method [snip] threw exception:
System.Data.Entity.Core.EntityException: The navigation property 'RequestItems' on entity of type 'Request' must implement ICollection<T> in order for Entity Framework to be able to track changes in collections.

Result StackTrace:
at System.Data.Entity.Core.Objects.DataClasses.EntityCollection1.CheckIfNavigationPropertyContainsEntity(IEntityWrapper wrapper)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.AddToNavigationProperty(IEntityWrapper wrapper)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.AddToNavigationPropertyIfCompatible(RelatedEnd otherRelatedEnd)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.EntityReference
1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.WalkObjectGraphToIncludeAllRelatedEntities(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.EntityCollection1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Entity.Core.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
at System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
at System.Data.Entity.Internal.Linq.InternalSet
1.<>c__DisplayClassd.<Add>b__c()
at System.Data.Entity.Internal.Linq.InternalSet1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet
1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
[snip]

ajcvickers wrote Jun 10, 2014 at 9:58 PM

@cnboland It looks like the collection is of an interface type, which is currently not supported by EF. The exception message could certainly be clearer in this case which is something I will bring up. You can vote for mapping to interfaces to be supported here: http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1272291-support-interfaces-as-entity-and-complex-types

Thanks,
Arthur

ajcvickers wrote Jun 10, 2014 at 10:00 PM

Note for triage: if this is the exception you get when mapping to interfaces then it is not very clear.

cnboland wrote Jun 30, 2014 at 8:54 PM

@ajcvickers, thanks for the follow up. This works correctly in EF5, which was the current version at the time we started our project in 2012, and have been using it since. We were all surprised to see such a breaking change.

I voted for and commented on the User Voice page.

cnboland wrote Jun 30, 2014 at 8:57 PM

Oh, and nice Sex Pistols, @ajcvickers. ;-)