sql server - Connection with 3 tables in Entity Framework -


i have 3 tables in sql database: movies, viewer , movies_viewer (= shows viewer saw movie).

the client insert viewer name , movie name , entity framework, want insert 3 tables have (with linq example), how should know id gets movie , viewer insert in third table?

should insert 2 tables , call function insert id's third? how have data?

this code - wrong this?

[webmethod] public void inserttodb(string viewername, string moviename) {     inserttoviewertbl(viewername);     inserttomoviestbl(moviename); }  private void inserttomoviestbl(string moviename) {     var db = new moviesentities1();     var movienametbl = new movienametbl();     movienametbl.moviename = moviename;      db.addtomovienametbls(movienametbl);     db.savechanges(); }  private void inserttoviewertbl(string viewername) {     var db = new moviesentities1();     var viewernametbl = new viewernamestbl();     viewernametbl.viewername = viewername;      db.addtoviewernamestbls(viewernametbl);     db.savechanges(); } 

you return objects, after adding them db, :

[webmethod] public void inserttodb(string viewername, string moviename)  {     var viewer = inserttoviewertbl(viewername);      var movie = inserttomoviestbl(moviename);     inserttomovies_viewer(viewer, movie); }  private movienametbl inserttomoviestbl(string moviename) {     var db = new moviesentities1();      var movienametbl = new movienametbl();     movienametbl.moviename = moviename;      db.addtomovienametbls(movienametbl);     db.savechanges();      return movienametbl ; }  private viewernamestbl inserttoviewertbl(string viewername) {     var db = new moviesentities1();      var viewernametbl = new viewernamestbl();     viewernametbl.viewername = viewername;      db.addtoviewernamestbls(viewernametbl);     db.savechanges();      return viewernametbl; }  private void inserttomovies_viewer(viewernamestbl viewer, movienametbl movie) {     var db = new moviesentities1();      var movieviewer = new movies_viewer();// or table called     movieviewer.viewer = viewer; // or column called     movieviewer.movie= movie; // or column called      db.addtomovies_viewer(movieviewer);     db.savechanges(); } 

but i'd advise in 1 method, won't create connection every time, , save changes every time :

[webmethod] public void inserttodb(string viewername, string moviename)  {     var db = new moviesentities1();      var movie = new movienametbl();     movienametbl.moviename = moviename;      db.addtomovienametbls(movienametbl);      var viewer = new viewernamestbl();     viewernametbl.viewername = viewername;      db.addtoviewernamestbls(viewernametbl);      var movieviewer = new movies_viewer();// or table called     movieviewer.viewer = viewer; // or column called     movieviewer.movie= movie; // or column called      db.addtomovies_viewer(movieviewer);     db.savechanges(); } 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -