Updating a disconnected LINQ object using MVC Framework RC1

This is a bit, but I have a client object returning to my controller. I just want to connect this object to the database again, is this possible? I know there is datacontext.customers.insertonsubmit (client), but is there an equivalent datacontext.customers.updateonsubmit (client) ???

+5
source share
3 answers

You want to use the attach method in the Customers table in the data context.

datacontext.customers.Attach(customer);

to reconnect it to the data context. Then you can use SubmitChanges()to update the values ​​in the database.

EDIT. , . , ASP.NET MVC UpdateModel TryUpdateModel, @Odd.

0

, LINQ-to-SQL.

, , , , , .

:

Attach , DataContext, , . DataContext , . Attach , . , , , .

, . , , - , , .

, , , "" "".

, , DataContext:

DataContext " ", . DataContext - . LINQ to SQL DataContext , .

, DataContexts - Attach() DataContext, . /, , Attach().

, , , , , .

+5

The client that you publish from the form will not have entity keys, therefore it cannot be well connected, and you also may not have every client field in the form so that all its fields cannot be set.

I would recommend using the TryUpdateModel method, in your action you will have to get the client from the database again and update it using the form column variables.

public ActionResult MySaveAction(int id, FormCollection form)
{
    Customer updateCustomer = _Repository.GetCustomer(id);

    TryUpdateModel(updateCustomer, "Customer", form);

    _Repository.Save(updateCustomer);
}

You will need to add all your own exception handling and checking, but this is a general idea.

+3
source

All Articles