EF4 Code First Adding a Entity with virtual navigation property null to a 1 to many -


i have following ef4 code first classes:

[serializable] public 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 addition of new wochangelogheader 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 @ point changelogrecords navigation property null null object reference error...

but if try add wochangelogheader without adding children, can subsequently reference 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 following error ?

the operation failed: relationship not changed because 1 or more of foreign-key properties non-nullable. when change made relationship, related foreign-key property set null value. if foreign-key not support null values, new relationship must defined, foreign-key property must assigned non-null value, or unrelated object must deleted.

it not let me savechanges on context when changelogrecords null either...

how add new entity parent in 1 many relationship ?

thanks greg

the problem may here:

public virtual wochangelogheader changelogheader { get; set; }  public int wochangelogheaderid { get; set; }  

both of these lines make reference header, first line needed.

try removing second line.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -