I have an EDMX containing Sentences
, and Words
, say and a Sentence
contains three Words
, say. Appropriate FK relationships e
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!