locked
connection with 3 tables in Entity RRS feed

  • Question

  • Hello,

    I have 3 tables in SQL dataBase: Movies, Viewer and Movies_Viewer (= that shows which viewer saw which movie).

    the client will insert Viewer name and Movie name and with the Entity I want to insert it to the 3 tables I have (with linq for example), but how should I know what ID gets the movie and the viewer to insert it in the third table?

    should I insert to the 2 tables and then call another function that insert the ID's to the third? how do I have all the data?

    please halp me!

    this is my code:

    Is it wrong to do like 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();
            }
        }



    • Edited by shirley111 Saturday, March 21, 2015 10:53 AM
    Saturday, March 21, 2015 10:06 AM

Answers