c# - Entity Framework creates new data in database instead of using exsisting one (even when I don't tell to do that) -


i'm facing strange problem. i'm making kind of dictionary, use 3 classes: word (for every single word), translation (that stores 2 words means same in different languages) , language (that stores information language).

my server code webapi project. here c# api controller , 2 functions used it:

//controller:  // post: api/translations [responsetype(typeof(string))] [httppost] public async task<ihttpactionresult> posttranslation(translationsviewmodel tvm) {     if (!modelstate.isvalid)     {         return badrequest(modelstate);     }      if (tvm.language1id == tvm.language2id)     {         return badrequest("languages have different");     }      translation translation = tvm.totranslation();      db.translations.add(translation);     await db.savechangesasync();      return ok("translation created succesfully"); }  public translation totranslation(translationsviewmodel tvm) {     translation translation = new translation {         wordslanguage1 = new list<word>(),         wordslanguage2 = new list<word>(),         tags = new list<tag>()     };      addwords(tvm.wordsl1, tvm.language1id).foreach(w => translation.wordslanguage1.add(w));     addwords(tvm.wordsl2, tvm.language2id).foreach(w => translation.wordslanguage2.add(w));     translation.reviewinterval = tvm.reviewinterval;     translation.notes = tvm.notes;     addtags(tvm.tags).foreach(o => translation.tags.add(o));      return translation; }  public list<word> addwords(string givenwords, int languageid) {     list<word> wordstoreturn = new list<word>();      char[] delimiters = new[] { ' ', ',', ';' };  // list of delimiters     string[] splittedarray = givenwords.split(delimiters, stringsplitoptions.removeemptyentries);      foreach (var word in splittedarray)     {         wordstoreturn.add(new word         {             content = word,             language = db.languages.find(languageid)         });     }      return wordstoreturn; } 

here models of c# app:

public class word {     public int id { get; set; }     public string content { get; set; }     public language language { get; set; }     public imagesset images { get; set; } //przy "get" wypadałoby wygenerować nowe obrazki, jeśli nie ma     public virtual icollection<translation> translations { get; set; }     public virtual icollection<tag> tags     {                 {             if (translations != null)             {                 icollection<tag> tags = new list<tag>();                  foreach (var translation in translations)                 {                     foreach (var tag in translation.tags)                     {                         tags.add(tag);                     }                 }                 return tags;             }             else             {                 return null;             }         }     } }  public class translation {     public int id { get; set; }     public virtual icollection<word> wordslanguage1 { get; set; } //słowo + synonimy     public virtual icollection<word> wordslanguage2 { get; set; } //słowo + synonimy     [datatype(datatype.date)]     public datetime? lastreviewed { get; set; }     [datatype(datatype.date)]     public datetime? nextreview     {                 {             if (lastreviewed != null)             {                 datetime somedate = (datetime)lastreviewed;                 return somedate.adddays(reviewinterval);             }             return datetime.now;         }     }     public int reviewinterval { get; set; }     public virtual icollection<tag> tags { get; set; }     public string notes { get; set; }     public int activeimage { get; set; }      public translationsviewmodel totvm()     {         translationtranslator tt = new translationtranslator();         return tt.totvm(this);     } }  public class translationsviewmodel {     public int id { get; set; }     /* id language1 */     public int language1id { get; set; }      /* id language2 */     public int language2id { get; set; }      /* word 1 , synonyms */     public string wordsl1 { get; set; }      /* word 2 , synonyms */     public string wordsl2 { get; set; }      /* tags separated comma or space */     public string tags { get; set; }      /* notes */     public string notes { get; set; }      public int reviewinterval { get; set; }      //changing "translation" class     public translation totranslation()     {         translationtranslator tt = new translationtranslator();         return tt.totranslation(this);     } }  public class language     {         public int id { get; set; }         public string name { get; set; }     } 

what totranslation() , translationviewmodel: because of structure of translation model, can't send through webapi client because there like: "self referencing loop".

so created other model: translationviewmodel can send client. totranslation() changes translationviewmodel translation.

what addwords(): changes 1 string of words separated commas, list of strings words. separate function, because i'm gonna check there if there similar word in db (to avert duplicates).

and problem?

strange thing happens here. see in addwords(), assign language every word. provide languageid, find in db , add word. if have languageid = 1, should have got word language id = 1.

the problem is: entity framework clones language id = 1 (creates new one, same no. 1) , assign new language word. instead of word language id = 1 word langauge id = 2.

so when create 10 new words, api creates 10 new languages named "english". want have 1 "english" language 10 words assigned it.

ps: (offtopic) using translationviewmodel best way send information client/javascript? doing correctly?


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -