3

I have a class like this:

public class Content {
   public string Id {get; set;} = "content/"
   public ContentJob? Job {get; set;}
}

public class ContentJob {
   public string JobId {get; set;} = string.Empty;
}

I can create the content and if the job is there it will persist. with Store/SaveChanges.

But what I can't do is update (or more accurately set) ContentJob on Content and have it detect that there was a change. (HasChange(instance) == false) and SaveChanges doesn't update the database.

Why? And how do I make it detect the change?

(incidentally I tried using C# 9 records in hopes that because it automatically does deep object equality that this would solve it and no, it doesn't)

James Hancock
  • 3,348
  • 5
  • 34
  • 59

1 Answers1

2

I created an unit-test based on your question, and it works as expected.

 [Fact]
        public void ShouldWork()
        {
            using (var store = GetDocumentStore())
            {
                string id = string.Empty;
                using (var session = store.OpenSession())
                {
                    var c = new Content();
                    session.Store(c);
                    session.SaveChanges();
                    id = session.Advanced.GetDocumentId(c);

                    var entity = session.Load<Content>(id);
                    entity.Job = new ContentJob()
                    {
                        JobId = "123"
                    };
                    
                    Assert.True(session.Advanced.HasChanged(entity));
                    session.SaveChanges();
                }

                Assert.False(string.IsNullOrEmpty(id));

                using (var session = store.OpenSession())
                {
                    var entity = session.Load<Content>(id);
                    Assert.NotNull(entity.Job);
                    Assert.Equal("123", entity.Job.JobId);
                }
            }
        }
        public class Content
        {
            public string Id { get; set; } = "content/";
            public ContentJob? Job { get; set; }
        }

        public class ContentJob
        {
            public string JobId { get; set; } = string.Empty;
        }
garay
  • 636
  • 3
  • 5
  • I think the only difference is that the initial content in question that we're generating is done in a previous session. And then it's retrieved using a filtered query based on security that just applies a Linq where and then returns FirstOrDefaultAsync(). Could that be the problem? – James Hancock Jan 24 '21 at 14:00
  • I tried to get the entity using a query, and the unit test passed. It would be the best if could send a failing unit-test to: https://groups.google.com/forum/?utm_source=footer#!forum/ravendb – garay Feb 16 '21 at 08:15