EF4 Code First Adding a Entity with virtual navigation property null to a 1 to many
I have the following EF4 Code First classes:
[Serializable]
pu开发者_StackOverflow社区blic class WOChangeLogHeader
{
[Key]
public int WOChangeLogHeaderId { get; set; }
public DateTime tadded { get; set; }
public virtual WorkOrderHeader WO { get; set; }
public int WorkOrderHeaderId { get; set; }
[MaxLength(50)]
public string chng_type { get; set; }
[MaxLength(50)]
public string chng_process { get; set; }
public int chng_by { get; set; }
public virtual ICollection<WOChangeLog> ChangeLogRecords {get;set;}
}
[Serializable]
public class WOChangeLog
{
[Key]
public int WOChangeLogId { get; set; }
public DateTime tadded { get; set; }
public virtual WOChangeLogHeader ChangeLogHeader { get; set; }
public int WOChangeLogHeaderId { get; set; }
[MaxLength(50)]
public string chng_field { get; set; }
public string old_value { get; set; }
public string new_value { get; set; }
}
And setup the addition of a new WoChangeLogHeader like so:
private void addWOChangeLogRecord(string chgField, string oldVal, string newVal, WorkOrderHeader wo)
{
WOChangeLog log = new WOChangeLog();
log.chng_field = chgField.Trim();
log.old_value = oldVal == null ? "-NULL VALUE-" : oldVal.Trim();
log.new_value = newVal == null ? "-NULL VALUE-" : newVal.Trim();
log.tadded = DateTime.Now;
if (CurrentWOChangeLogHeader == null)
{
CurrentWOChangeLogHeader = new WOChangeLogHeader();
CurrentWOChangeLogHeader.WO = wo;
CurrentWOChangeLogHeader.WorkOrderHeaderId = wo.WorkOrderHeaderId;
CurrentWOChangeLogHeader.chng_by = -2;
CurrentWOChangeLogHeader.chng_process = "WindowsService";
CurrentWOChangeLogHeader.chng_type = "Auto-Update";
CurrentWOChangeLogHeader.tadded = DateTime.Now;
}
CurrentWOChangeLogHeader.ChangeLogRecords.Add(log); // Error here
}
However at this point ChangeLogRecords navigation property is null so I get a null object reference error...
But if I try to add the WOChangeLogHeader without adding any children, so that I can subsequently reference it like so:
WOChangeLog log = new WOChangeLog();
log.chng_field = chgField.Trim();
log.old_value = oldVal == null ? "-NULL VALUE-" : oldVal.Trim();
log.new_value = newVal == null ? "-NULL VALUE-" : newVal.Trim();
log.tadded = DateTime.Now;
log.ChangeLogHeader = CurrentWOChangeLogHeader;
then I get the following error ?
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
It will not let me SaveChanges on the Context when the ChangeLogRecords is null either...
How to add a new Entity that is the parent in a 1 to many relationship like this ?
Thanks Greg
The problem may be here:
public virtual WOChangeLogHeader ChangeLogHeader { get; set; }
public int WOChangeLogHeaderId { get; set; }
Both of these lines make a reference to the header, but only the first line is needed.
Try removing the second line.
精彩评论