c# - DbContext.Save on parent with child objects throws "Conflicting changes detected" -


i have parent/child hierarchy of want insert new parent dbcontext , have automatically persist child objects. relationship one-to-many, each parent can @ 0 or more columns.

but whenever call dbcontext.save(parent) receive 'conflicting changes detected. may happen when trying insert multiple entities same key.'. when strip parent of child columns saves fines, i'm assuming related child objects not having primary key set. how tell entityframework save hierarchy properly?

my classes:

public class exceltemplate {     public int id { get; set; }     public string name { get; set; }     public int firstdatarow { get; set; }     public virtual icollection<templatecolumn> columns { get; set; }      public exceltemplate()     {         columns = new list<templatecolumn>();     } }  public class templatecolumn {     public int id { get; set; }     public int index { get; set; }     public int metrictypeid { get; set; }     public virtual metrictype metrictype { get; set; }      public int templateid { get; set; }     public virtual exceltemplate template { get; set; } } 

and configurations:

public class exceltemplateconfiguration : entitytypeconfiguration<exceltemplate> {     public exceltemplateconfiguration()     {         haskey(t => t.id)             .property(t => t.id)             .hasdatabasegeneratedoption(databasegeneratedoption.identity);          property(t => t.name)             .isrequired();          hasmany(t => t.columns)             .withrequired(c => c.template)             .hasforeignkey(c => c.templateid);     } }  public class templatecolumnconfiguration : entitytypeconfiguration<templatecolumn> {     public templatecolumnconfiguration()     {         haskey(c => c.id)             .property(c => c.id)             .hasdatabasegeneratedoption(databasegeneratedoption.identity);          hasrequired(tpl => tpl.metrictype)             .withrequireddependent();     } } 

  1. do exceltemplate.id , templatecolumn.id have identity specification? setting id property properly?

  2. are setting exceltemplate property of templatecolumn objects exceltemplate parent?

something like:

exceltemplate t = new exceltemplate(); //init template code...skip  templatecolumn c = new templatecolumn(); //init template column code...skip c.exceltemplate = t;//this line asking if doing.  

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -