EF Core: AddRange(list) runs wrong overload

c# entity-framework entity-framework-core

Question

Microsoft.EntityFrameworkCore.DbContext (for version 1.1.0) has two version of AddRange (and others similar methods):

public virtual void AddRange([NotNullAttribute] IEnumerable<object> entities);
public virtual void AddRange([NotNullAttribute] params object[] entities);

Second just casts to IEnumerable<object>.

When I write:

IReadOnlyCollection<Entity> list = ...
context.AddRange(list);

second overload runs, and casts array of object to IEnumarable<object>, where single item is IReadOnlyCollection<Entity>. It then pass to StateManager.GetOrCreateEntry where it treated as entity itself. I'm not sure this not work really, just found my tests failed because of that piece:

dbMock.Setup(x => x.AddRange(It.IsAny<object[]>()))
                .Callback<object[]>(xs =>
                {
                    foreach (var entity in xs) // entity is IReadOnlyCollection<Entity> here

What I do wrong?

1
0
2/2/2017 5:49:26 PM

Popular Answer

You can cast the list itself to an IEnumerable<object>:

context.AddRange(list.Cast<object>());
0
2/2/2017 5:57:52 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow