简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM