I think this could be a bug but thought I might be missing something that could explain the behavior. Any help would be appreciated.
### Technical details
EF Core version: Microsoft.EntityFrameworkCore (2.0.1)
Database Provider: Microsoft.EntityFrameworkCore.SqlServer (2.0.1)
Database: SQL Server 2008
Operating system: Windows 10
IDE: Visual Studio 2017 Community Edition (15.4.4)
I've got a situation in a C# console application where I call Update on my DbContext (location is a LocationParty):
_context.Update(location);
The only change in the record is to the EndDate Field. Just prior to the call to SaveChangesAsync() examining _context.ChangeTracker shows that the entity is marked as modifed. All of the 43 other entities being tracked are marked as Unchanged.
Watch Window Image showing Modified Entity Status
The return value from SaveChangesAsync() is 1 indicating that one entity had changes. The SQL executed is a delete statement rather than an update which results in the record being removed rather than updated:
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (28ms) [Parameters=[@p0='8431'], CommandType='Text', CommandTimeout='30']
SET NOCOUNT ON;
DELETE FROM [LocationParties]
WHERE [ID] = @p0;
SELECT @@ROWCOUNT;
The code invoking the database changes (using the Repository Pattern) is shared between this console application and an MVC application. When the same update location code is called from the MVC application the record is properly updated. Note that when run by the MVC controller there is only the one entity being tracked when SaveChangesAsync() is called and that entity is also marked as Modified. In the console application there are 44 entities being tracked with only the entity of interest marked as modified. I looked to see if there was perhaps another instance of the entity being tracked in a Delete status but there is no such entity.
Update:
I may have figured out what is going on but don't know if the behavior is to be expected.
The LocationParty entity is in a one to many relationship with the Party entity (one Party has multiple locations). If I remember right I was getting some EF errors regarding tracking objects that were already being tracked (in my MVC app) when I first got started with EF. The Location Party has a Foreign Key to the Party entity (PartyID) and a navigation property to the Party entity. In the update method I had see that Party navigation property to null prior to the update to eliminate this problem. The foreign key was still set to point to the Party.
I assume that EF is keying off of the navigation property being null to infer that the Party was deleted and therefore the associated location should be deleted. The Party was not deleted by the update. I can't explain why it worked for the MVC app but not the Console app.
I commented out the code the set the Party navigation property to null and the location updated properly in both the console and MVC app.
My code was kind of weird but I assumed that setting the Foreign Key to point to a valid entity would be all that was required.