How To Update EF 4 Entity In ASP.NET MVC 3?

后端 未结 6 1933
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-09 07:26

I have 2 projects - a class library containing an EDM Entity Framework model and a seperate ASP.NET MVC project.

I\'m having problems with how your suppose to edit and s

6条回答
  •  再見小時候
    2021-02-09 08:03

    This is probably a little late to help answer the question, but maybe it will help some one else. It seems to me that the main problem you are having is caused by the fact that the database context instance that reads an entity is disposed after a page is rendered. So when you try to save it, this is a new instance of a context, which therefore has no knowledge of the previous context call. Entity Framework therefore has to update all rows in the database, because it has no way of knowing which rows were changed.

    This being said, the best method that I have found for saving all of the old data, and updating what I want to change, is to make a new call to the database first, setting all of the required information for a Model (or whatever you are using to store your data) to what is currently in the DB. Then making the necessary changes to that information, and then saving the result to the DB.

    You can look at the tutorial on Implementing Basic CRUD here for more information: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

提交回复
热议问题