How to use short-living context?

2 posts, 1 answers
  1. Froggie
    Froggie avatar
    29 posts
    Member since:
    Jan 2012

    Posted 22 Jan 2014 Link to this post

    Hi!
    I want to use short-living contexts, but I'm not able to achieve the saving of changed objects.
    Can you help me?

    This is my current code:
    internal void RefreshView()
       {
                List<CustomModel> availableCustomModels;
                using (var dbContext = new Context())
                {
                    availableCustomModels = dbContext.CustomModels.ToList();               
                }
     
                viewController.RefreshView(availableCustomModels);
            }
     
     private void viewController_Save(object sender, CustomModelEventArgs<CustomModel> e)
            {
                using (var dbContext = new Context())
                {
                    var modelToSave = (from x in dbContext.CustomModels
                                      where x.Id.Equals(e.Model.Id)
                                      select x).FirstOrDefault();
     
                    //TODO implement saving the changes
                    if (modelToSave != null)
                    {                   
                        dbContext.SaveChanges();
                    }
                }
     
                RefreshView();
            }
     
    private void viewController_Delete(object sender, CustomModelEventArgs<CustomModel> e)
            {
                using (var dbContext = new Context())
                {
                    var modelToDelete = (from x in dbContext.CustomModels
                                        where x.Id.Equals(e.Model.Id)
                                        select x).FirstOrDefault();
     
                    if (modelToDelete != null)
                    {
                        dbContext.Delete(modelToDelete);
                        dbContext.SaveChanges();
                    }
                }
     
                RefreshView();
            }
  2. Answer
    Boyan
    Admin
    Boyan avatar
    100 posts

    Posted 24 Jan 2014 Link to this post

    Hi Sebastian,

    As I reviewed your code I noticed that you could manually synchronize the properties of modelToSave with the corresponding values coming from the event parameter e.Model and thus updating the entity, like that:

    private void viewController_Save(object sender, CustomModelEventArgs<CustomModel> e)
    {
        using (var dbContext = new Context())
        {
            var modelToSave = (from x in dbContext.CustomModels
                              where x.Id.Equals(e.Model.Id)
                              select x).FirstOrDefault();
     
            //TODO implement saving the changes
            if (modelToSave != null)
            {             
                modelToSave.Id = e.Model.Id;
                // ...
                // The rest of the properties
                // ...
                dbContext.SaveChanges();       
            }
        }
     
        RefreshView();
    }

    As an alternative approach you could choose to work with attached/detached copies of your entities. This would allow you to avoid manual synchronization in your code.  For example:

    private void viewController_Save(object sender, CustomModelEventArgs<CustomModel> e)
    {
        if(e.Model != null
        {
            using (var dbContext = new Context())
            {
                dbContext.AttachCopy(e.Model);
                dbContext.SaveChanges();
            }
     
            RefreshView();
        }
    }

    For your convenience I have prepared a small sample project fully demonstrating the later approach. Please find it attached.

    Additionally I would recommend you going through our sample kit containing a number of applications involving various technologies and application structures.

    Should you have any further questions please let us know. 

    Regards,
    Boyan
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
  3. DevCraft banner
Back to Top