Trouble Mocking EntityEntry
I have some code that uses EntityEntry to prevent some properties from being edited during an update.
var entry = _dbContext.Scenarios.Update(scenario);
entry.Property(s => s.CreatedDate).IsModified = false;
entry.Property(s => s.CreatedBy).IsModified = false;
This works fine in production, but the implementation of EntityEntry is making it difficult to mock for testing purposes. I have followed the example shown in #27110 to no avail. Here is my mocking code:
var baseEntityType = new RuntimeEntityType(
nameof(BaseEntity), typeof(BaseEntity), sharedClrType: false, model: new(),
baseType: null, discriminatorProperty: null, ChangeTrackingStrategy.Snapshot,
indexerPropertyInfo: null, propertyBag: false, discriminatorValue: null);
var internalEntry = new InternalEntityEntry(
new Mock<IStateManager>().Object,
new RuntimeEntityType(
nameof(Models.V2.Scenario), typeof(Models.V2.Scenario), sharedClrType: false, model: new(),
baseType: baseEntityType, discriminatorProperty: null, ChangeTrackingStrategy.Snapshot,
indexerPropertyInfo: null, propertyBag: false, discriminatorValue: null),
scenario);
return new EntityEntry<Models.V2.Scenario>(internalEntry);
BaseEntity is in fact a base class of Scenario, so I tried creating a base type in this way to solve the issue. The CreatedDate property is a part of BaseEntity. The CreatedBy property is not. The below error is thrown for either property.
Exception thrown: 'System.InvalidOperationException' in Microsoft.EntityFrameworkCore.dll: 'The property 'Scenario.CreatedBy' could not be found. Ensure that the property exists and has been included in the model.'
at Microsoft.EntityFrameworkCore.Metadata.IReadOnlyEntityType.GetProperty(String name)
at Microsoft.EntityFrameworkCore.Metadata.IEntityType.GetProperty(String name)
at Microsoft.EntityFrameworkCore.ChangeTracking.PropertyEntry..ctor(InternalEntityEntry internalEntry, String name)
at Microsoft.EntityFrameworkCore.ChangeTracking.PropertyEntry`2..ctor(InternalEntityEntry internalEntry, String name)
at Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1.Property[TProperty](Expression`1 propertyExpression)
Can anyone tell me what's wrong here?
EF Core version: 7.0.16 Database provider: Microsoft.EntityFrameworkCore.Sqlite Target framework: net6 Operating system: MacOS Sonoma IDE: VS Code 1.86.1
Related to #27110
@ajcvickers Is that in response to me, or just general reference? I did tag that same issue in my original message, so I'm assuming it's the latter.
@brandonsmith86 It was a note for the team.
@ajcvickers I am still confused by your response. If I'm understanding correctly, your response was for my information, but I cited in my original post that I followed the solution in that issue, and it doesn't work for me.
@brandonsmith86 I work for the EF team at Microsoft. I made a note on the issue to consider the related issue when we triage this issue. I did not see at the time that you had already referenced that issue in your post because I didn't read it in detail, I just knew from scanning it that it was either the same root cause as an existing issue, or if not the same, then likely closely related. I expect we will decide to combine them into one issue on the backlog, but that is for the triage meeting to decide.
@ajcvickers Thank you for that clarification. Are you able to tell me if there is something I can do in current state to get this to work, or if this is something that will require a change in EF source?
@brandonsmith86 There is probably some way to get it to work, likely using EF Core internal types, but it would take some investigation to figure that out, and it could well break in future releases. We really need to address this by changes in EF.