troubleshooting Question

Why is Entity Framework creating A new Row in my table each time I save changes

Avatar of advfinance
advfinanceFlag for United Kingdom of Great Britain and Northern Ireland asked on
ASP.NETVisual Basic.NET
9 Comments1 Solution464 ViewsLast Modified:
I can't see the wood through the trees with this one. I'm pretty sure the answer is staring me in the face but I'm just missing the obvious.

This is my code to create a Customer WebSession. The Customer Web Session object is an EF Code First object (For testing purposes I drop the DB each time I run this and seed it with a bit of sample data). On going to the apply now screen, the user is asked to enter some compulsory details. There are only about 6 required fields in the database including the id and the ApplyNow page covers these.

        'GET: /Home/ApplyNow
        Function ApplyNow() As ActionResult
            Dim customerWebSession = New CKCustomerWebSession
            customerWebSession.GeneratedCode = CodeGenerator.CodeCheckUnique()
            Return View(customerWebSession)
        End Function


On the post request, I check that the model is valid and if it is then I add my WebSession object to the context and submit the change to the database. I then redirect the user to a new action method on a seperate controller and pass in the Web Session Object.


        'POST: /Home/ApplyNow
        <HttpPost()>
        Function ApplyNow(WebSession As CKCustomerWebSession) As ActionResult
            If ModelState.IsValid Then
                db.WebSessions.Add(WebSession)
                db.SaveChanges()
                Return RedirectToAction("Page1", "Session", WebSession)
            Else
                Return View(WebSession)
            End If
        End Function

Here I simply GET a view and pass in my existing Web Session Object. This view allows the user to enter more details and submit them.

        'GET: /Session/Page1
        Function Page1(WebSession As CKCustomerWebSession) As ActionResult
            Return View(WebSession)
        End Function

Now this is where the problem seems to kick in, on the POST back of the new user data. Instead of adding it to the existing WebSession object it creates a brand new one and adds a new row into my database. I want it to update the existing row instead.

        'POST: /Session/Page1
        <HttpPost()>
        <ActionName("Page1")>
        Function Page1Post(WebSession As CKCustomerWebSession) As ActionResult
            If ModelState.IsValid Then
                    db.WebSessions.Add(WebSession)
                    db.SaveChanges()
                Return RedirectToAction("Page2", "Session", WebSession)
            Else
                Return View(WebSession)
            End If
        End Function

Any help that can be provided on this would be much appreciated.

Thanks
ASKER CERTIFIED SOLUTION
Gautham Janardhan

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros