This is a migrated thread and some comments may be shown as answers.

How to use short-living context?

1 Answer 36 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Froggie
Top achievements
Rank 1
Froggie asked on 22 Jan 2014, 08:53 AM
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();
        }

1 Answer, 1 is accepted

Sort by
0
Accepted
Boyan
Telerik team
answered on 24 Jan 2014, 11:47 AM
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!
Tags
Getting Started
Asked by
Froggie
Top achievements
Rank 1
Answers by
Boyan
Telerik team
Share this question
or