views:

128

answers:

0

Hi.

I have a data model like this:

[Table(Name = "DayWorks")]
public class DayWork
{
 [Column(DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
 public int ID { get; set; }
 ...
 /* no any EntitySet here for extensionable model purposes (really need) */
}
[Table(Name = "Documents")]
public class Document
{
 [Column(DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
 public int ID { get; set; }
 ...
 [Column(CanBeNull = false)]
 public int AddedByWorkID { get; set; }
 private EntityRef<Work> _addedByWork;
 [Association(ThisKey = "AddedByWorkID", OtherKey = "ID", IsForeignKey = true)]
 public Work AddedByWork
 {
  get { return this._addedByWork.Entity; }
  set { this._addedByWork.Entity = value; }
 }
 [Column(CanBeNull = true)]
 public int? CopiedFromDocumentID { get; set; }
 private EntityRef<Document> _copiedFromDocument;
 [Association(ThisKey = "CopiedFromDocumentID", OtherKey = "ID", IsForeignKey = true)]
 public Document CopiedFromDocument
 {
  get { return this._copiedFromDocument.Entity; }
  set { this._copiedFromDocument.Entity = value; }
 }
}

this code does not work:

Document doc = new Document();
...
doc.AddedByWork = context.Works.Single(...);
doc.CopiedFromDocument = context.Documents.Single(...);
context.Documents.Add(doc);
context.SubmitChanges();

SubmitChanges throws InvalidOperation Exception (An attempt was made to remove a relationship between Work and Document. However, one of the relationship's foreign keys (Document.AddedByWorkID) cannot be set to null.

That is because Document (that I try to set to CopiedFromDocument property) has right AddedByWorkID, but has no AddedByWork entity (it's null).

Have I to load it manually? ... or maybe I do something wrong.