Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have a problem when updating related entities. So my Document entity has a collection of Customers. When Im adding a new document I can add as many customers as I want, but when Im updating only scalar properties are updated.

This is my code for updating a document...
public static bool UpdateDocument(Document documentToUpdate)
{
    using (MyDbContext = new MyDbContext())
    {
        // attaching the entity does not help
        if (context.Entry(documentToUpdate).State == EntityState.Detached)
            context.Documents.Attach(documentToUpdate);

        context.Entry(documentToUpdate).State = EntityState.Modified;
        context.SaveChanges();
        return true;
    }
}


Evrything regarding document manipulation, adding and removing customers is done in another layer/project, so my question is how shuld I proceed with this? Or how do I update the related customer collection here in this method? Or shuld I use a different method just for customer manipulation.

Something like

public static void UpdateCustomersOnDoc(Document doc)
{
    using (MyDbContext context = new MyDbContext())
    {
        Document tempDoc = DocumentCRUD.GetByID(doc.Id);
        // remove the customers from the document (database)
        tempDoc.Customers.Clear();

        // add customers from the modified document
        foreach(var cust in doc.Customers)
        {
            docTemp.Customers.Add(cust);
        }
        // save changes to the context
        context.SaveChanges();
    }
}
Posted
Comments
Raul Iloc 26-Mar-14 5:07am    
Did you try as I indicated in my solution?

1 solution

The problem is that you are trying to update the Document object that come from your view, and this is not OK, because it just a copy of your document from your database; and also the deleting and the reassigning the customers for each document update is not OK.

So in your controller class you should first search search by using Document ID (or its primary key) the current document, then update this object (that was read from the database context) with data that come from the view (user input that is cached in Document param from your method) and finally save it to the database. This should solve your problem!

So you should have:

C#
public static bool UpdateDocument(Document documentToUpdate)
{
    using (MyDbContext = new MyDbContext())
    {
        Document doc = MyDbContext.FirstOrDefault(d=> d.ID == documetToUpdate.ID)
        //
        //
        CopyUserInputData(documentToUpdate, doc); //You should implement this method that save user input in the given "doc" object!   
        context.SaveChanges();
        return true;
    }
}
 
Share this answer
 
v3
Comments
pykos 27-Mar-14 2:48am    
Well the problem with this approach is that I allredy map my view model to entity model in another project, and I feel that mapping evrything again is a bit redundant...
Raul Iloc 27-Mar-14 3:26am    
Is not about mapping, is about how to use EF in the context of MVC application.
Each time the code is executed on your controller at response to an user action from your view, the controller class is recreated and also its properties and used objects, in this case a new EF context object is recreated. So the EF entity object that come from your model was created into a EF context that does not exist anymore on your server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900