none
EF4 - STE - Don't save related object RRS feed

  • Question

  • HI,

    I have a "Client" STE objet which contains a "Project" TrackerCollection.

    In my code, I have : 

    Client client = ClientManager.CreateClient();
    
    Operation op = OperationManager.CreateOperation(client);
    
    client.Name = "clientModified";
    
    ClientManager.Save(client);
    


    In OperationManager.CreateOperation(client); I have :

    public Operation CreateOperation(Client client)
    {
        Operation op = new Operation();
        op.Name = "operation1";
        op.Client = client;
    
        client.Operations.Add(op);
    
        return op;
    }
    

    And to save my client, I have : 

                using (var context = new KC.DAL.KCModelContainer())
                {
                    string setName = typeof(T).Name + "s";
                    context.ApplyChanges<T>(setName, entity);
                    context.SaveChanges();             
                    entity.AcceptChanges();
                }
    


     

    Now, when I save client object, the new Operation is saved, too...

    I would like save only the client attribute (name, ...) (not all Operations collection, which will be saved later). Is it possible ?

    Thanks

    Wednesday, October 5, 2011 8:42 AM

Answers

  • My code was an example. IN reality, I have one button which add an operation and and other which can save only "Client" attributes.

    My solution is :

     - Save operation collection,

     - clear collection,

     - save Client

     - add operation

     

    Somethink like :

     

    List<Operation> lstOP = new List<Operation>();
    foreach(Operation op in client.Operations)
    {
        lstOp.Add(op);
    }
    
    client.Operations.Clear();
    
    context.Clients.ApplyChanges<Client>(client);
    context.SaveChanges();
    
    foreach(Operation op in lstOp)
    {
       client.Operations.Add(op);
    }
    

    What do you think about it ?

     


    Friday, October 7, 2011 8:34 AM

All replies

  • You just need move >>Operation op = OperationManager.CreateOperation(client);
    after the context savechanges method.

     


    I am fish.
    Friday, October 7, 2011 2:12 AM
  • My code was an example. IN reality, I have one button which add an operation and and other which can save only "Client" attributes.

    My solution is :

     - Save operation collection,

     - clear collection,

     - save Client

     - add operation

     

    Somethink like :

     

    List<Operation> lstOP = new List<Operation>();
    foreach(Operation op in client.Operations)
    {
        lstOp.Add(op);
    }
    
    client.Operations.Clear();
    
    context.Clients.ApplyChanges<Client>(client);
    context.SaveChanges();
    
    foreach(Operation op in lstOp)
    {
       client.Operations.Add(op);
    }
    

    What do you think about it ?

     


    Friday, October 7, 2011 8:34 AM
  • In fact it's not the good solution, because "AcceptChange" reload related object.When I made the first Accept, all is ok, but when I launch the second Accept and save, he saved my new operation...
    Friday, October 7, 2011 10:22 PM