简体   繁体   中英

Entity framework 6 : generic method AddOrUpdate

I spend so much time trying implement a generic method to add or update a entity with a related entity (relation one to many) but I am stuck...

The method must receives 2 parameters, the first is the parent, second is the child. The goal is to save child entity into parent (adding if does not exist or update)

There is the generic method signature:

    public static bool AddOrUpdate<T,U>(T ItemToSave,U ItemRelated, int ID) where T : class where U : class
    {
        using (var context = new dbContext())
        {                
            var parent = context.Set<T>().Find(ID);
            if (parent == null) return;
            // how to retrieve now the child (ItemRelated) from the parent (ItemToSave) in order to add into it or update ?

            return context.SaveChanges() > 0;
        }
    }

This method is located in a static class 'Service' and I want to be able to call Service.AddOrUpdate(Order _order, OrderLine _orderline, _order.OrderId) from any class.

I am stuck at retrieving the child from parent and to add or update into it.

Can anyone please help me to achieve that ?

Your ItemRrelated should implement some Interface with parentId property. Then you can just add it to DbSet if it's not exists already.

var existingItemRelated == context.Set<U>().SingleOrDefault(ir => ir.ParentId == ItemRelated.ParentId && (/*your criteria to determine if child item equals the one in DB*/));

then if it's not exists add it or edit if exists.

EDIT

also if you don't want to have common interface for entities with parent items you can pass expression to this method determining if children entities has the same parent

public static bool AddOrUpdate<T, U>(T ItemToSave, U ItemRelated, int ID, Expression<Func<U,bool>> sameParentsExpression) where T : class where U : class
{
    using (var context = new dbContext())
    {
        var parent = context.Set<T>().Find(ID);
        if (parent == null) return false;
        // how to retrieve now the child (ItemRelated) from the parent (ItemToSave) in order to add into it or update ?
        var existingItemRelated = context.Set<U>()
                                    .Where(sameParentsExpression)
                                    .SingleOrDefault(/*your criteria to determine if child item equals the one in DB*/);

        return context.SaveChanges() > 0;
    }
}

and call for it like

AddOrUpdate(order, orderline, order.OrderId, ol => ol.OrderId == order.OrderId)

With the given constraints, it just isn't possible. Think it through - the only constraint you have given is that each must be a class. This doesn't guarantee that there is a parent child relationship, let alone how that relationship will be defined.

Generics aren't magic. They simply allow you to deal with items in a predefined way. And the definition of interaction MUST exist before we write a generic method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM