Avatar of advfinance
advfinance
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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

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

Open in new window



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

Open in new window


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

Open in new window


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

Open in new window


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

Thanks
Visual Basic.NETASP.NET

Avatar of undefined
Last Comment
advfinance

8/22/2022 - Mon
Gautham Janardhan

calling db.WebSessions.Add(WebSession) will always add a new row,  if object state is not added then cann applycurrentvalues instead of add
advfinance

ASKER
Thank you gauthampj. However, when I try to call applycurrentvalues() on my dbcontext instance "db" it's not being recognised. Do you know why this could be?

Thanks
Gautham Janardhan

shud be on websessions


db.WebSessions.applycurrentvalues(WebSession)
Your help has saved me hundreds of hours of internet surfing.
fblack61
advfinance

ASKER
It isn't showing there either unfortunately.
Gautham Janardhan

which version of entity framework are u using ?
advfinance

ASKER
Version 5
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Gautham Janardhan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
advfinance

ASKER
Just discovered this myself and have implemented it (although your way of doing it is cleaner than what I decided on). Many thanks
advfinance

ASKER
Helped me work through the problem step by step and understand how to solve it rather than just giving me the answer.