Avatar of advfinance
advfinanceFlag 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
Avatar of Gautham Janardhan
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
Avatar of advfinance
advfinance
Flag of United Kingdom of Great Britain and Northern Ireland image

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
shud be on websessions


db.WebSessions.applycurrentvalues(WebSession)
Avatar of advfinance
advfinance
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

It isn't showing there either unfortunately.
which version of entity framework are u using ?
Avatar of advfinance
advfinance
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Version 5
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of advfinance
advfinance
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Just discovered this myself and have implemented it (although your way of doing it is cleaner than what I decided on). Many thanks
Avatar of advfinance
advfinance
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo