Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting the IsModified flag to false on a property leaves original value untouched so the change is detected again #7798

Closed
AndersKung opened this issue Mar 7, 2017 · 1 comment
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@AndersKung
Copy link

AndersKung commented Mar 7, 2017

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//Microsoft.EntityFrameworkCore.1.1.1

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var context = new Context();
            var user = new Users();
            user.Id = 1;

            context.Update(user);

            user.UserName = "name";
            user.City = "city";

            OnUpdating(context, user);

            // ↓↓↓Exception↓↓↓
            // The property 'UserName' on entity type 'Users' is marked as null, but this cannot be saved because the property is marked as required.
            context.SaveChanges(); 
            
        } 

        static void OnUpdating(Context context, Users user)
        {
            context.Entry(user).Property(x => x.UserName).IsModified = false;
            var isModified = context.Entry(user).Property(x => x.UserName).IsModified; // still true
        }
    }

    public class Users
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string City { get; set; }
    }
    public class Context : DbContext
    {
        public virtual DbSet<Users> Users { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Users>(b =>
            {
                b.HasKey(r => r.Id);

                b.Property(x => x.UserName).IsRequired();
            });
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Initial Catalog=Test;Trusted_Connection=True;MultipleActiveResultSets=true");
        }
    } 
}

http://stackoverflow.com/questions/12661881/exclude-property-on-update-in-entity-framework

@ajcvickers
Copy link
Member

Notes for triage: Confirmed this is a bug, but not a regression. The original value is being left as-is when setting the IsModified flag. This causes DetectChanges to detect that the current and original values are different, and so the property is marked as modified again.

Workaround: When setting the original value to be the same as the current value before marking the property as not modified. For example:

var propertyEntry = context.Entry(user).Property(x => x.UserName);
propertyEntry.OriginalValue = user.UserName;
propertyEntry.IsModified = false;

@divega divega changed the title Is this a BUG? Setting the IsModified flag to false leaves original untouched so the change is detected again Mar 7, 2017
@divega divega changed the title Setting the IsModified flag to false leaves original untouched so the change is detected again Setting the IsModified flag to false leaves original values untouched so changes are detected again Mar 7, 2017
@divega divega changed the title Setting the IsModified flag to false leaves original values untouched so changes are detected again Setting the IsModified flag to false on a property leaves original value untouched so the change is detected again Mar 7, 2017
@ajcvickers ajcvickers self-assigned this Mar 8, 2017
@ajcvickers ajcvickers added this to the 2.0.0 milestone Mar 8, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Apr 19, 2017
ajcvickers added a commit that referenced this issue Jun 23, 2017
Issues #7798, #8265, #8465

These issues were due to incorrect handling of nulls/conceptual nulls for required but nullable properties, in particular when attaching an entity with a required property that starts off null.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 26, 2017
ajcvickers added a commit that referenced this issue Jun 26, 2017
Issues #7798, #8265, #8465

These issues were due to incorrect handling of nulls/conceptual nulls for required but nullable properties, in particular when attaching an entity with a required property that starts off null.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

2 participants