Entity Framework 5 Updating a Record

前端 未结 8 1145
悲哀的现实
悲哀的现实 2020-11-22 08:06

I have been exploring different methods of editing/updating a record within Entity Framework 5 in an ASP.NET MVC3 environment, but so far none of them tick all of the boxes

相关标签:
8条回答
  • 2020-11-22 09:03

    Depending on your use case, all the above solutions apply. This is how i usually do it however :

    For server side code (e.g. a batch process) I usually load the entities and work with dynamic proxies. Usually in batch processes you need to load the data anyways at the time the service runs. I try to batch load the data instead of using the find method to save some time. Depending on the process I use optimistic or pessimistic concurrency control (I always use optimistic except for parallel execution scenarios where I need to lock some records with plain sql statements, this is rare though). Depending on the code and scenario the impact can be reduced to almost zero.

    For client side scenarios, you have a few options

    1. Use view models. The models should have a property UpdateStatus(unmodified-inserted-updated-deleted). It is the responsibility of the client to set the correct value to this column depending on the user actions (insert-update-delete). The server can either query the db for the original values or the client should send the original values to the server along with the changed rows. The server should attach the original values and use the UpdateStatus column for each row to decide how to handle the new values. In this scenario I always use optimistic concurrency. This will only do the insert - update - delete statements and not any selects, but it might need some clever code to walk the graph and update the entities (depends on your scenario - application). A mapper can help but does not handle the CRUD logic

    2. Use a library like breeze.js that hides most of this complexity (as described in 1) and try to fit it to your use case.

    Hope it helps

    0 讨论(0)
  • 2020-11-22 09:06

    There are some really good answers given already, but I wanted to throw in my two cents. Here is a very simple way to convert a view object into a entity. The simple idea is that only the properties that exist in the view model get written to the entity. This is similar to @Anik Islam Abhi's answer, but has null propagation.

    public static T MapVMUpdate<T>(object updatedVM, T original)
    {
        PropertyInfo[] originalProps = original.GetType().GetProperties();
        PropertyInfo[] vmProps = updatedVM.GetType().GetProperties();
        foreach (PropertyInfo prop in vmProps)
        {
            PropertyInfo projectProp = originalProps.FirstOrDefault(x => x.Name == prop.Name);
            if (projectProp != null)
            {
                projectProp.SetValue(original, prop.GetValue(updatedVM));
            }
        }
        return original;
    }
    

    Pros

    • Views don't need to have all the properties of the entity.
    • You never have to update code when you add remove a property to a view.
    • Completely generic

    Cons

    • 2 hits on the database, one to load the original entity, and one to save it.

    To me the simplicity and low maintenance requirements of this approach outweigh the added database call.

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