zzzprojects / GraphDiff

GraphDiff is a library that allows the automatic update of a detached graph using Entity Framework code first.
https://entityframework-graphdiff.net/overview
MIT License
333 stars 101 forks source link

OwnedCollection crashes for custom collection types #155

Open Jerther opened 8 years ago

Jerther commented 8 years ago

When calling OwnedCollection on a custom collection, I get the following error:

GraphDiff requires the collection to be either IEnumerable or T[]

The custom collection is something like:

class FooCollection : List<Foo>

The error seems to from this code:

CollectionGraphNode
private Type GetCollectionElementType()
{
    if (Accessor.PropertyType.IsArray)
    {
        return Accessor.PropertyType.GetElementType();
    }

    if (Accessor.PropertyType.IsGenericType)
    {
        return Accessor.PropertyType.GetGenericArguments()[0];
    }

    throw new InvalidOperationException("GraphDiff requires the collection to be either IEnumerable<T> or T[]");
}

Is there a workaround for this?

Jerther commented 8 years ago

For now I've figured I could declare my class like so as a workaround:

class FooCollection<T> : List<Foo>
  where T : Foo

But I think we'll agree that we'd rather not do that... :)

kotylo commented 8 years ago

Just modify the source code and add:

if (Accessor.PropertyType.BaseType.IsGenericType)
{
    return Accessor.PropertyType.BaseType.GetGenericArguments()[0];
}

throw new InvalidOperationException("GraphDiff requires the collection to be either IEnumerable<T> or T[] or derived from GenericType");

I've added simple test for this and others were still green. Also it worked on my DB.

Jerther commented 8 years ago

Thanks!!

I know you stopped supporting the project, but is there any chance for this to make it to the NuGet package?

Dvornik commented 8 years ago

@kotylo Thank you! Could you add a pull request with this modification and test, please?

kotylo commented 8 years ago

@Dvornik, I just checked this on my collection. The fix may not be that simple, but it works for my simple entities. Created pull request here https://github.com/refactorthis/GraphDiff/pull/161