2013-12-17 23 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]; 
    ... 
相关问题