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?
You can cast the list itself to an IEnumerable<object>
:
context.AddRange(list.Cast<object>());