Solved

Entity loop error

Posted on 2011-02-11
7
521 Views
Last Modified: 2012-05-11
When I try to execute the follwoing code I get the follwoing error.

An object with the same key already exists in the ObjectStateManager. The existing object is in the Modified state. An object can only be added to the ObjectStateManager again if it is in the added state. How can I solve the problem?

Pls help.

               for (int i = 0; i < uploadedFiles.Count; i++)
                {
                    HttpPostedFile userPostedFile = uploadedFiles[i];

                    if (userPostedFile.ContentLength > 0)
                    {
                        myWorkDocs.Filename = guidResult;
                        myWorkDocs.UploadedBy = User.Identity.Name;
                        myWorkDocs.UploadedOn = DateTime.Now;

                        myEntity2.AddToWorkOrderDocs(myWorkDocs); <-----error here
                        myEntity2.SaveChanges();
                       
                    }
                       
                }

Open in new window

0
Comment
Question by:ayha1999
  • 3
  • 3
7 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34873575
It is difficult to say without seeing more of the code.

Where is this object created, myWorkDocs?
This looks like a function you created AddToWorkOrderDocs, can you post the code for it?

From the error it seems that you are trying to insert a record that already exist in the ObjectContext with the same primary key.
0
 
LVL 9

Accepted Solution

by:
gdupadhyay earned 300 total points
ID: 34873596
Your code is not complete,

Please see following URL might help you.  Its explain security for file upload and complete method.

http://msdn.microsoft.com/en-us/library/aa479405.aspx

http://www.dotnetspider.com/resources/21345-Uploading-Multiple-files-using-ASP-NET.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfile.aspx#Y761

Can post complete code?


Good Luck
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34873647
I am trying to upload file and at the same time writing file names to the table. uploading is working fine. I can upload multiple files at the same time but only one record(with file name get inserted in the table). I want to write all file names. pls help me.

thanks

            WMSEntities myEntity2 = new WMSEntities();

              WorkOrderDoc myWorkDocs;
            myWorkDocs = new WorkOrderDoc();

  for (int i = 0; i < uploadedFiles.Count; i++)
                {
                    HttpPostedFile userPostedFile = uploadedFiles[i];

                    if (userPostedFile.ContentLength > 0)
                    {
                        string guidResult = System.Guid.NewGuid().ToString();
                        userPostedFile.SaveAs(filepath + guidResult + extension);

                        myWorkDocs.WorkorderId = cboWOId.SelectedItem.Text;
                        myWorkDocs.Filename = guidResult;
                        myWorkDocs.UploadedBy = User.Identity.Name;
                        myWorkDocs.UploadedOn = DateTime.Now;

                        myEntity2.AddToWorkOrderDocs(myWorkDocs);
                        myEntity2.SaveChanges();
                       
                    }
                       
                }

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34873761
What is this, "AddToWorkOrderDocs"? A method you created? if so please post.

Which one of the following is the primary key?

  myWorkDocs.WorkorderId
  myWorkDocs.Filename
  myWorkDocs.UploadedBy
  myWorkDocs.UploadedOn
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34874053
primary key is not here bcz it an identity column.

and AddToWorkOrderDocs is a built in function to add records. without this myentity.SaveChanges will not work.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34874107
Please post code for AddToWorkOrderDocs.
0
 
LVL 7

Author Comment

by:ayha1999
ID: 34874480
void wmsEntity.AddToWorkOrderDocs(workOrderDoc WorkOrderDoc)
Deprecated method for adding a new object to workOrderDocs entity set.Consider using the .Add method of ObjectSet<T> property set instead.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SlingPlayer alternatives 1 35
Not showing page correctly 3 31
Data is not showing from images 15 38
What .NET URL re-routing tool did I use? 2 37
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now