locked
EF5 - Generic repository - WCF entity update RRS feed

  • Question

  • I've got an EF5 Code First deployment with Entities being passed over WCF to be updated (SOA implementation), basically I have an object which is passed to a service, worked on and then returned, the ContainerAction entity and values in the Container entities are being updated by the worker service. The problem is when saving, only the ContainerAction is getting updated. I have seen a few examples of how to manage it, either update individually or mark each ActionableContainer & Container as Modified, this however doesnt work with a Generic Repository approach. Is there a way to achieve this?

    The structure of the objects is: (cut down for posting purposes)

        public class ContainerAction
        {
            public int  ContainerActionId { get; set; }

            public List<ActionableContainer> Containers { get; set; }
        }

        public class ActionableContainer
        {
            public int ActionableContainerId { get; set; }

            [Required]
            public int ContainerId { get; set; }

            [ForeignKey("ContainerId")]
            public Container Container { get; set; }
        }

        public class Container
        {                
            public int ContainerId { get; set; }

            [MaxLength(50)]
            public string Alias { get; set; }
        }


    The code I am using to save changes... (extract from Generic Repository)

        public void Update(T entity, object[] keyValues)
            {
                if (entity == null)
                {
                    throw new ArgumentException("Entity is null");
                }            

                var entry = _context.Entry(entity);

                if (entry.State == EntityState.Detached)
                {
                    var set = _context.Set<T>();                

                    T attachedEntity = set.Find(keyValues);

                    if (attachedEntity != null)
                    {
                        var attachedEntry = _context.Entry(attachedEntity);
                        attachedEntry.CurrentValues.SetValues(entity);
                    }
                    else
                    {
                        entry.State = EntityState.Modified;
                    }
                }
            }

    Any help on this would be much appreciated, I dont really want to have to implement a lot of additional non-generic repositories or loop 500 odd containers updating each.

    Many thanks
    Monday, July 8, 2013 11:23 AM

Answers

  • I have written a blog post about how to implement a generic data access layer using Entity Framework in a disconnected scenario: http://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework/

    You can change the state of a passed in entity by setting the DbContext.Entry(item).State property as described in my post. The key to know the state of an entity when it comes from a disconnected source is to have the entity objects track their own state by explicitly setting the state on the client side before passing them to the business layer or by exposing separate method for adding, updating and removing entities.

    • Marked as answer by Bob Shen Tuesday, July 16, 2013 8:19 AM
    Monday, July 8, 2013 1:13 PM