You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following code runs in production but throws an InvalidOperationException in the unit test when DBSet (Materials) is a MockDBSet. Here is the setup for the mocks:
var items = new List<Material> {
new Material { Id = 1, Pieces = 10 },
new Material {Id = 2, Pieces = 10 }
};
var mockItems = new MockDBSet<Material>()
.SetupSeedData(items)
.SetupAddAndRemove()
.SetupLinq();
var mockContext = new Mock<MyContext>();
mockContext.Setup(o=>o.Materials).Returns(mockItems.Object);
The mockContext is then passed to a service. Here is the code within the service that throws the exception:
var items = _context.Materials.ToList();
var newItem = new Material();
var oldItem = items.First();
_context.Materials.Add(newItem);
_context.Entry(newItem).CurrentValues.SetValues(oldItem);
Here is the exception:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Member 'CurrentValues' cannot be called for the entity of type 'Material' because the entity does not exist in the context. To add an entity to the context call the Add or Attach method of DbSet.
The text was updated successfully, but these errors were encountered:
The following code runs in production but throws an InvalidOperationException in the unit test when DBSet (Materials) is a MockDBSet. Here is the setup for the mocks:
The mockContext is then passed to a service. Here is the code within the service that throws the exception:
Here is the exception:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Member 'CurrentValues' cannot be called for the entity of type 'Material' because the entity does not exist in the context. To add an entity to the context call the Add or Attach method of DbSet.
The text was updated successfully, but these errors were encountered: