Aggregate Root support in Entity Framework

前端 未结 2 1604
青春惊慌失措
青春惊慌失措 2021-02-13 12:52

How can we tell Entity Framework about Aggregates?

  1. when saving an aggregate, save entities within the aggregate
  2. when deleting an aggregate, delete entitie
2条回答
  •  醉梦人生
    2021-02-13 13:28

    I wrote GraphDiff specifically for this purpose. It allows you to define an 'aggregate boundary' on update by providing a fluent mapping. I have used it in cases where I needed to pass detached entity graphs back and forth.

    For example:

    // Update method of repository
    public void Update(Order order)
    {
        context.UpdateGraph(order, map => map
            .OwnedCollection(p => p.OrderItems);
    }
    

    The above would tell the Entity Framework to update the order entity and also merge the collection of OrderItems. Mapping in this fashion allows us to ensure that the Entity Framework only manages the graph within the bounds that we define on the aggregate and ignores all other properties. It supports optimistic concurrency checking of all entities. It handles much more complicated scenarios and can also handle updating references in many to many scenarios (via AssociatedCollections).

    Hope this can be of use.

提交回复
热议问题