none
SaveChanges() not working in specific use RRS feed

  • Question

  • I have a db first EF 4.0 project. SaveChanges() does not work, although I have no problem getting data via context. This is a Drag - Drop from tree to tree, which has worked before.

    Here are 2 code blocks that read data successfully but do not update / create rows and SaveChanges() fails, never returning to the call; try catch does nothing?

    Create New Object (if doesn't exist)...

    public CategoryGroup VerifyCategoryGroup(int categoryId, int groupId) { var categoryGroup = (from CategoryGroup cg in _db.CategoryGroups where cg.CategoryID == categoryId && cg.GroupCategoryID == groupId select cg).FirstOrDefault(); if (categoryGroup == null) { var newCategoryGroup = new CategoryGroup(); newCategoryGroup.CategoryID = categoryId; newCategoryGroup.GroupCategoryID = groupId; newCategoryGroup.CreatedOn = DateTime.Now; _db.CategoryGroups.AddObject(newCategoryGroup); SaveChanges();
    categoryGroup = newCategoryGroup; } return categoryGroup; }

    Where SaveChanges() is implemented as ...

            public int SaveChanges()
            {
                var changeCount = 0;
                try
                {
                    changeCount = _db.SaveChanges();
                }
                catch (OptimisticConcurrencyException)
                {
                   throw;
                }
    
                return changeCount;
            }

    Here is an update call ....

            public void UpdateCategoryExtensionData(Category category)
            {
                var priorCategory = (from Category c in _db.Categories
                                     where c.CategoryID == category.CategoryID
                                     select c).FirstOrDefault();
    
                if(priorCategory != null)
                {
                    priorCategory.ExtensionData = category.ExtensionData;
                    SaveChanges();
                }
            }
    
    I did update from 3.5 to 4 prior to this. Dropping my old edmx and creating a new one. The Entity Linq versions are all 4.0.30319.

    Help me keep my sanity.

    Abott

    Monday, December 30, 2013 7:59 PM

Answers

  • Hello Abbott,

    It is strange that because I made a test that is similar with yours and it worked fine:

      using (DataBaseFirstDBEntities db = new DataBaseFirstDBEntities())
    
                {
    
    
                    Order order = (from o in db.Orders
    
                                   select o).FirstOrDefault();
    
    
                    if (order == null)
    
                    {
    
                        order = new Order();
    
    
                        order.OrderID = 1;
    
                        order.OrderCode = "1";
    
                        order.OrderName = "1";
    
    
                        db.Orders.AddObject(order);
    
    
                        db.SaveChanges();
    
                    }
    
                }
    

    The difference is that I do not use a global Objectcontext, so I suggest writing the query in a using block to see whether the SaveChange() will work.

    Or if it is possible, you can upload your program to https://skydrive.live.com so that we can download it and test it to figure out why it does not work for you.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 31, 2013 7:25 AM
    Moderator