2013-12-17 57 views
0

我在asp.net 3.5中有Web應用程序。我只想將記錄添加到3個表格中,並且一個表格包含與表格主鍵值關聯的多個文件詳細信息。如何使用linq2sql將多個表中的記錄插入多個表

欲瞭解更多的信息,我有這樣的代碼:

protected void submit_Click(object sender, EventArgs e) 
{ 
    if (Page.IsValid) 
    { 
     using (DataClassesDataContext db = new DataClassesDataContext()) 
     { 
      int user_id = 0; 
      var query = from u in db.Users 
         where u.Username == (String)Session["Username"] 
         select new 
         { 
          Id = u.Id 
         }; 
      foreach (var item in query) 
      { 
       if (item != null) 
       { 
        user_id = int.Parse(item.Id.ToString()); 
        break; 
       } 
      } 
      Post myPost = new Post(); 
      myPost.Title = txt_ComName.Text.Trim(); 
      myPost.Category_id = int.Parse(DDL_Categorynames.SelectedItem 
           .Value.ToString()); 
      myPost.Description = txt_ComName1.Text.Trim(); 
      myPost.User_id = user_id; 
      myPost.ToUser_id = user_id; 
      if(file_upload.HasFile) 
      { 
       myPost.IsFileAttached = true; 
      } 
      else 
      { 
       myPost.IsFileAttached = false; 
      } 
      db.Posts.InsertOnSubmit(myPost); 
      db.SubmitChanges(); 
      int newId = myPost.Id; 
      Flag myFlag = new Flag(); 
      myFlag.IsRead = false; 
      myFlag.IsImportant = false; 
      myFlag.IsRemoved = false; 
      myFlag.User_id = user_id; 
      myFlag.Post_History_id = newId; 
      db.Flags.InsertOnSubmit(myFlag); 
      db.SubmitChanges(); 
      File myFile = new File(); 
      HttpFileCollection fileCollection = Request.Files; 
      for (int i = 0; i < fileCollection.Count; i++) 
      { 
       HttpPostedFile uploadfile = fileCollection[i]; 
       string fileName = Path.GetFileName(uploadfile.FileName); 
       string fileType = System.IO.Path.GetExtension(fileName) 
           .ToString().ToLower(); 
       myFile.Post_History_id = newId; 
       myFile.File_name = fileName; 
       myFile.File_ext = fileType; 
       myFile.File_Size = uploadfile.ContentLength.ToString(); 
       if (uploadfile.ContentLength > 0) 
       { 
        uploadfile.SaveAs(Server.MapPath("~/PostFiles/") 
                  + fileName); 
        db.Files.InsertOnSubmit(myFile); 
        db.SubmitChanges(); 
       } 
      } 
      Panel_AddNew.Visible = false; 
      Panel_ViewPostList.Visible = true; 
      this.FillGrid(); 
     } 
    } 
} 

然而,此方案第一檔爲所選已插入之後那麼第二個文件重複發生的錯誤,如:

Cannot add an entity that already exists.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Cannot add an entity that already exists.

Source Error:

Line 248: {
Line 249: uploadfile.SaveAs(Server.MapPath("~/PostFiles/") + FileName);
Line 250: db.Files.InsertOnSubmit(myFile);
Line 251: db.SubmitChanges();
Line 252: }

Source File: f:\EasyWeb\EndUser\Post_History.aspx.cs Line: 250

請幫我...

回答

0

foreach循環的每次迭代中創建一個新的File

for (int i = 0; i < fileCollection.Count; i++) 
{ 
    File myFile = new File(); 
    HttpPostedFile uploadfile = fileCollection[i]; 
    ... 
相關問題