Complex Entity Framework linked-graphs issue: how to limit change set / break the graph?

前端 未结 1 1347
灰色年华
灰色年华 2021-01-21 16:53

I have an EDMX containing Sentences, and Words, say and a Sentence contains three Words, say. Appropriate FK relationships e

1条回答
  •  无人及你
    2021-01-21 17:36

    One alternative that does seem to work is to rename and hide the database generated properties (e.g. SubjectP), maintain a separate private copy of the Word that is set, and then fix it up during save:

    in Sentence:

        private Word subject;
        ...
    
        public Word Subject { get {return this.subject ?? this.SubjectP;} set {this.subject = value; }}
    
        public void FixSelfUp()
        {
            if (this.subject != null && subject != this.SubjectP) this.SubjectP = this.subject;
        ...
    

    And in SaveChanges() ...

        var items = this.context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added | System.Data.EntityState.Modified);
        // now iterate over them, find any Sentences and call to fix them up.
    

    This makes the Sentence behave just like it was intended to behave, allows setting and getting of each Word on it and doesn't allow EF to join them all together into one big graph.

    But surely there's a better way than this!

    0 讨论(0)
提交回复
热议问题