public void StoreSongsData(AddSongPostViewModel songdata) { string uniqueFileName = null; if (songdata.Image != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + songdata.Image.FileName; string filepath = Path.Combine(uploadsFolder, uniqueFileName); songdata.Image.CopyTo(new FileStream(filepath, FileMode.Create)); } Song songs = new Song { SongName = songdata.SongName, ReleaseDate = songdata.ReleasedDate, //DateTime.ParseExact(songdata.ReleasedDate.ToString(), "yyyy-MM-dd", null), ImagePath = uniqueFileName, Rating = 0 }; Console.WriteLine(songs.ReleaseDate); context.Songs.Add(songs); context.SaveChanges(); var sid = songs.SongId; List <Songs_Artists> song_artist_details = new List <Songs_Artists>(); foreach (var item in songdata.Artists) { song_artist_details.Add(new Songs_Artists { SongId = sid, ArtistId = Convert.ToInt32(item) }); } context.AddRange(song_artist_details); context.SaveChanges(); }