entity framework - Adding Unchanged items to an Object Graph full of Added items -


i have domain object called option. part of larger quiz schema. first 2 primary keys (id) in table correlate values “true” , “false” of text column. after that, items in table of no consequence scope of question. significance of 2 rows “true” , “false” re-used many times over.

also note option table identity (auto increment) table. , have following in optionmap file:

this.haskey(t => t.id); this.property(t => t.id).hasdatabasegeneratedoption(databasegeneratedoption.identity); 

obviously, don’t want keep adding true , false table every time new question true , false options added quiz.

in javascript, @ client, have coded such when new quiz created, , true and/or false added options question, id of 1 , 2 added object sent server (for true , false respectively - recall top 2 rows explained @ top of post). when question not using either of option, id of option sent server undefined.

at server, in service, have written following code able detect true or false has been added option , attempts use existing stored value true/false (as applicable) option table:

foreach (var question in newquestions) {            _quizrepository.addquestion(question);     var options = question.questionwithoptions.select(q => q.option).tolist();      // loop edge case. true , false common answers. rather storing them on , on again,     // loop re-use stored answers true , false, complies quiz schema.     foreach (var option in options)     {         if (option.text.equals("true", stringcomparison.ordinalignorecase) ||             option.text.equals("false", stringcomparison.ordinalignorecase))         {             var storedoption = _quizrepository.context.option.single(o => o.id == option.id);             foreach (var questionwithoption in question.questionwithoptions)             {                 if (questionwithoption.option.id == storedoption.id)                 {                     questionwithoption.option = storedoption;                     _quizrepository.context.entry(questionwithoption.option).state = entitystate.unchanged;                 }             }         }                         }      _quizrepository.savechanges(); } 

i have tried set state unchanged , modified, every time call savechanges, adds new rows true , false. , noted above, trying avoid.

any in figuring out how can save these new questions using existing options "true" , "false" appreciated.

edit - added domain classes

question:

public partial class question {     public question()     {         questionwithoptions = new list<questionwithoption>();         quizwithquestions = new list<quizwithquestion>();     }     public int id { get; set; }     public string text { get; set; }     public int? idtimelimit { get; set; }     public questiontype idtype { get; set; }      public virtual icollection<questionwithoption> questionwithoptions { get; set; }     public virtual icollection<quizwithquestion> quizwithquestions { get; set; } } 

questionwithoption:

public partial class questionwithoption {     public int id { get; set; }     public int idquestion { get; set; }     public int idoption { get; set; }     public bool correctanswer { get; set; }     public string letter { get; set; }     public virtual option option { get; set; }     public virtual question question { get; set; } } 

option:

public partial class option {     public option()     {         this.questionwithoptions = new list<questionwithoption>();     }      public int id { get; set; }     public string text { get; set; }     public virtual icollection<questionwithoption> questionwithoptions { get; set; } } 

add question method

this toggles between following 2 lines of code (i'm trying , everything)

    public void addquestion(question question)     {         //((ttswebinarscontext) db).question.attach(question);         ((ttswebinarscontext) db).question.add(question);     } 

edit - final comment

let me add how ridiculous situation is. in same operation, adding new options option table (instead of using existing options 1 , 2), values inserted questionwithoption table correct values i.e. 1 , 2. so, operation not sound. there redundant values being added option table not fks in operation. i'm starting understand fans of nhibernate.

another option wouild without messing around states (but bit more ressource intensive): iterate on option, replace default answers,remember indices in options of those. once out of iteration, remove items index remembered them.

i way because not want meddle in affairs of ef tooo much...

edit:

or better: add field not mapped option class , set if want remove option. after replace list.removeall(f => f.flag);

another edit:

and think have loops mixed up. - if see structures correctly - rewrite to:

void storequstionsoptimized( question newquestions context _quizrepository ) {     var defaultoptiontrue = xxx;     var defaultoptionfalse = xxx;      foreach (var question in newquestions)     {              foreach (var questionwithoption in question.questionwithoptions)         {             if (questionwithoption.option.text.equals("true", stringcomparison.ordinalignorecase) )             {                 questionwithoption.option = defaultoptiontrue;             }             else if( questionwithoption.option.text.equals("false", stringcomparison.ordinalignorecase))             {                  questionwithoption.option = defaultoptionfalse;             }         }         _quizrepository.addquestion(question);     }      _quizrepository.savechanges();         } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -