Strongly-Typed ASP.NET MVC with Entity Framework

后端 未结 5 541
孤街浪徒
孤街浪徒 2020-12-16 09:01

This code fails to actually save any changes:

//
// POST: /SomeType/Edit/5

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Guid id, SomeType Model)
{         


        
5条回答
  •  情深已故
    2020-12-16 09:19

    I found none of the above seemed to work for me, but the MSDN guide example did which is where you get the original item from the context, and then attach the updated item.

    Second example:

    http://msdn.microsoft.com/en-us/library/bb896248.aspx#Mtps_DropDownFilterText

    private static void ApplyItemUpdates(SalesOrderDetail updatedItem){
    // Define an ObjectStateEntry and EntityKey for the current object.
    EntityKey key;
    object originalItem;
    
    using (AdventureWorksEntities advWorksContext =
        new AdventureWorksEntities())
    {
        try
        {
            // Create the detached object's entity key.
            key = advWorksContext.CreateEntityKey("SalesOrderDetail", updatedItem);
    
            // Get the original item based on the entity key from the context
            // or from the database.
            if (advWorksContext.TryGetObjectByKey(key, out originalItem))
            {
                // Call the ApplyPropertyChanges method to apply changes
                // from the updated item to the original version.
                advWorksContext.ApplyPropertyChanges(
                    key.EntitySetName, updatedItem);
            }
    
            advWorksContext.SaveChanges();
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
    

    }

提交回复
热议问题