Skip to content

Commit

Permalink
Rejecting changes when marking a property as not modified
Browse files Browse the repository at this point in the history
Issue #5645
  • Loading branch information
ajcvickers committed Jun 3, 2016
1 parent f41d2fc commit 89663bf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,6 @@ public virtual void SetPropertyModified(
bool changeState = true,
bool isModified = true)
{
// TODO: Restore original value to reject changes when isModified is false
// Issue #742

var propertyIndex = property.GetIndex();
_stateData.FlagProperty(propertyIndex, PropertyFlag.Unknown, false);

Expand Down Expand Up @@ -258,6 +255,11 @@ public virtual void SetPropertyModified(

if (changeState)
{
if (!isModified
&& property.GetOriginalValueIndex() != -1)
{
SetProperty(property, GetOriginalValue(property), setModified: false);
}
_stateData.FlagProperty(propertyIndex, PropertyFlag.TemporaryOrModified, isModified);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Specification.Tests;
using Xunit;

Expand Down Expand Up @@ -121,6 +118,41 @@ public void Can_set_and_clear_modified()
Assert.False(new PropertyEntry(entry, "Primate").IsModified);
}

[Fact]
public void Can_reject_changes_when_clearing_modified_flag()
{
var entity = new Wotty { Id = 1, Primate = "Monkey", Marmate = "Bovril" };

var entry = TestHelpers.Instance.CreateInternalEntry(
BuildModel(),
EntityState.Unchanged,
entity);

var primateEntry = new PropertyEntry(entry, "Primate");
primateEntry.OriginalValue = "Chimp";
primateEntry.IsModified = true;

var marmateEntry = new PropertyEntry(entry, "Marmate");
marmateEntry.OriginalValue = "Marmite";
marmateEntry.IsModified = true;

Assert.Equal(EntityState.Modified, entry.EntityState);
Assert.Equal("Monkey", entity.Primate);
Assert.Equal("Bovril", entity.Marmate);

primateEntry.IsModified = false;

Assert.Equal(EntityState.Modified, entry.EntityState);
Assert.Equal("Chimp", entity.Primate);
Assert.Equal("Bovril", entity.Marmate);

marmateEntry.IsModified = false;

Assert.Equal(EntityState.Unchanged, entry.EntityState);
Assert.Equal("Chimp", entity.Primate);
Assert.Equal("Marmite", entity.Marmate);
}

[Fact]
public void Can_get_name_generic()
{
Expand Down Expand Up @@ -431,6 +463,7 @@ private class Wotty
{
public int Id { get; set; }
public string Primate { get; set; }
public string Marmate { get; set; }
}

private class FullyNotifyingWotty : HasChangedAndChanging
Expand Down Expand Up @@ -518,15 +551,15 @@ private abstract class HasChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private abstract class HasChangedAndChanging : HasChanged, INotifyPropertyChanging
{
public event PropertyChangingEventHandler PropertyChanging;

protected void OnPropertyChanging([CallerMemberName]string propertyName = "")
protected void OnPropertyChanging([CallerMemberName] string propertyName = "")
=> PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
}

Expand Down

0 comments on commit 89663bf

Please sign in to comment.