ravendbravendb5

RavenDb changes to Property (nullable) on Entity that is a class doesn't save


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)


Solution

  • 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;
            }