Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

ravendb5 - 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)

question from:https://stackoverflow.com/questions/65852599/ravendb-changes-to-property-nullable-on-entity-that-is-a-class-doesnt-save

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...