asp.net mvc 4 - Entity framework code first, delete childs by updating parent -


as entity framework states, "code first", here go code first...

public class basemodel {     [key]     public guid id { get; set; }     public datetime datecreated { get; set; }     public datetime datechanged { get; set; }      public basemodel()     {         this.id = guid.newguid();         this.datecreated = datetime.now;         this.datechanged = datetime.now;     } }  public class association: basemodel {     public string name { get; set; }     public string type { get; set; }      public virtual list<rule> rules { get; set; }      public association()         : base()     {     } }  public class rule: basemodel {     [foreignkey("association")]     public guid associationid { get; set; }      //[required]     public virtual association association { get; set; }      //[required]     public string name { get; set; }      public string expression { get; set; }      public virtual list<action> actions { get; set; }      public rule()         : base()     {     } }  public class action: basemodel {     public string name { get; set; }      public string actiontype { get; set; }      [foreignkey("rule")]     public guid ruleid { get; set; }      public virtual rule rule { get; set; }      public int order { get; set; }      public action()         : base()     {     } } 

so these 4 model classes using entity framework code first. each inherit baseclass, have id guid primary key.

an association has list of rules. (rule has fk association) rule has list of actions. (action has fk rule)

what change , save upwards class = association. example when deleting rule, code work:

 public actionresult deleterule(guid assid, guid ruleid)     {         association ass = this.datacontext.associations.firstordefault(a => a.id == assid);         ass.rules.removeall(r => r.id == ruleid);          this.datacontext.savechanges();          return redirecttoaction("index");     } 

on context.savechanges giving me 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.'

this error occurs when deleting action.

is there way change upper (association) object , changing things association. not want context.rules.remove(...) or context.actions.remove(...)

here's source: http://server.thomasgielissen.be/files/mvctesting.zip need vs2012, nuget packages included in zip , should able build , run project.

thanks in advance feedback!

greetz, thomas

i want fix issue, should store relations through junction tables. don't think can achieve need, model.

however if put junction table(or entity) between entities, can remove child objects , update parent object.

for example, put junction entity between association , rule:

public class associationrule: basemodel {     public guid associationid { get; set; }     public guid ruleid { get; set; }      [foreignkey("associationid")]     public virtual association association { get; set; }     [foreignkey("ruleid")]     public virtual rule rule { get; set; }              public association()         : base()     {     } } 

now, can remove rule association:

public actionresult deleterule(guid assid, guid ruleid) {     associationrule assr = this.datacontext         .associationruless         .firstordefault(ar => ar.associationid == assid && ar.ruleid == ruleid);      this.datacontext.associationrules.remove(assr);     this.datacontext.savechanges();      return redirecttoaction("index"); } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -