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

后端 未结 6 1924
佛祖请我去吃肉
佛祖请我去吃肉 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 07:41

    I found this tutorial on the asp.net website to be very helpful: Getting Started with EF using MVC - Updating Related Data

    The method to pay attention to is TryUpdateModel which will update the related model in the "standard" manner (setting the entity to a modified state and passing everything in) and then will let you customize how certain properties are updated.

    I ran into problems with entity keys and this helped me get past those.

    0 讨论(0)
  • 2021-02-09 07:50

    I would retrieve the user from the database and update that entity. EF can't just magically know which values are supposed to be modified and which aren't.

    If you're concerned about concurrency issues you would need to store the timestamp in the view - usually as a hidden form value.

    0 讨论(0)
  • 2021-02-09 07:51

    OK, after some trial and error, I look to have found a solution. Here is my updated Update method in the UserRepository:

    public void Update(User user)
    {
        using (this.Context)
        {
            var tempUser = new User { usr_id = user.usr_id };
    
            this.Context.Users.Attach(tempUser);
            this.Context.ApplyCurrentValues("Users", user);
            this.Context.SaveChanges();
        }
    }
    

    Some of other examples I tried were very close to the above, but just missed the mark.

    0 讨论(0)
  • 2021-02-09 07:58

    Please look at my Update method on the following question: Partially updating object with EF Code First and ASP.NET MVC This works nice, even when I dislike, that I have to specify the field names.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-09 08:06

    http://forums.asp.net/p/1697685/5032858.aspx/1?Re+MVC3+edits+to+records+using+Entity+Framework+not+saving+to+the+database

    Look the link above I think is a very similar case (with a solution). Good luck

    0 讨论(0)
提交回复
热议问题