Click here to Skip to main content
15,888,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, Im working on a MVC app.

When I call context.SaveChanges to update a specific records. The update is not registered in the database. I do not get any runtime error either. All in notice is that my Records is not updated. I still see the same values. Insert Functionality work Perfectly.

What seems to be the problem?

here is my code

public Admission Update(int stuid)
{
VDData.VidyaDaanEntities context = new VDData.VidyaDaanEntities();
VDData.Student_Master studentmaster = new VDData.Student_Master();

studentmaster.Student_ID = stuid; //here i will get the ID.
studentmaster.Student_First_Name = this.FirstName;
studentmaster.Student_Middle_Name = this.MiddleName;
studentmaster.Student_Last_Name = this.LastName;
studentmaster.Student_Address_1 = this.Address;
studentmaster.Student_Address_2 = this.Address2;
studentmaster.Student_City = this.City;
studentmaster.Student_State = this.State;
studentmaster.Student_Pin_Code = this.Pincode;

context.SaveChanges(); // here it wont give any kind of error. it runs sucessfuly.
}
Posted
Updated 14-May-13 19:15pm
v3
Comments
ZurdoDev 14-May-13 7:31am    
You'll need to do a trace, either SQL profiler or debug your code, or both.
Yuriy Loginov 14-May-13 9:27am    
try
context.Table.State = EntityState.Modified;
context.SaveChanges();
harshal7689 15-May-13 1:56am    
i trying ur code but it give me error at "State". for that can i need to add any reference into my code
Mohammed Hameed 15-May-13 1:21am    
Debug & verify all the values at runtime.
harshal7689 15-May-13 1:55am    
i already done the debugging as well as verify the values but the table not getting update.

Hello,

Please read this articles:
http://msdn.microsoft.com/en-us/library/bb896271.aspx[^]
http://stackoverflow.com/questions/8056761/context-savechanges-not-working[^]

Your object is probably detached from the context object.

Bye.
 
Share this answer
 
Be aware, that you can create an object of a type in your model, without adding it to the data store. And you are actually doing this.
In the model you have a collection of VDData.Student_Master. It should be context.Student_Masters - but it depends on the naming convention you have used. This collection represents the table. You have to add the object to the collection if you want to persist it. And if you want to update an existing one, you have to get it from the collection with Linq to EF's. Just by setting the id of the object won't change it in the data store, even less if it is outside the collection.

See: http://www.dotnetcurry.com/ShowArticle.aspx?ID=619[^]
And this has nothing to do with MVC.
 
Share this answer
 
v4
Hi
I think in case of update you should read the student master first rather than creating a new object.

so you should not do the following:
VDData.Student_Master studentmaster = new VDData.Student_Master();


Rather you should read it first from context like:
VDData.Student_Master studentmaster = Read from context using Student_ID ;


rest of your code may remain same.

Hope this will help you.
 
Share this answer
 
Thank guys to help me and i am happy that my code is working Properly
i did a some changes and my code works perfectly.

Old code:
VDData.Student_Master studentmaster = new VDData.Student_Master();


New Code:
Student_Master studentmaster = context.Student_Master.Single(p => p.Student_ID == stuid);


due to above line my Records updated into table.
 
Share this answer
 
Comments
Zoltán Zörgő 15-May-13 3:40am    
Feel free to accept all the answers that helped you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900