Entity Framework not saving changes?

If you are after the insert/update functionality you have to cover both cases.

Up vote 0 down vote favorite share g+ share fb share tw.

I've got an MVC web application that uses SQL Server 2008 as a back end database along with the Entity Framework. The application is working fine and pulling data from the database just fine. My problem is, when it does an update to the data, it doesn't appear to be saving it.

I am using the follow function: public void SaveProduct(Product product) { if (product. ProductID == 0) { context.Products. Add(product); } context.SaveChanges(); // Breakpoint here } This function is defined in my repository code.

I set a breakpoint on the line commented above and the application is breaking at the line, so I know its hitting that line and the changes are all good in the context object. No error occurs, EF simply isn't saving the changes for some reason. I believe my connection string is correct, since its pulling the data just fine, but here it is just in case: Anyone have any ideas on what could cause this?

C# asp. Net-mvc-3 sql-server-2008 entity-framework-4.1 link|improve this question asked Mar 4 at 21:45icemanind5,4831250 97% accept rate.

There should just before any savechanges are done be a login entry in profiler from EF. – Ricky. G Mar 4 at 22:04 Have you checked if you hitting context.Products.

Add(product); If not, nothing is being saved. – veblock Mar 4 at 22:09 @Ricky. G -- Good idea, I'll try that – icemanind Mar 4 at 22:16 @veblock - I don't have the view for adding products done yet, so I can't test that part yet – icemanind Mar 4 at 22:17.

If you are after the insert/update functionality you have to cover both cases: if (product. ProductID == 0) { context. Entry(product).

State = EntityState. Added; } else { context. Entry(product).

State = EntityState. Modified; } context.SaveChanges().

This change fixed my problem! Thanks! – icemanind Mar 4 at 22:19.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions