简体   繁体   English

实体框架6:通用方法AddOrUpdate

[英]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. 该方法必须接收2个参数,第一个是父级,第二个是子级。 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. 此方法位于静态类“服务”中,我希望能够从任何类中调用Service.AddOrUpdate(Order _order,OrderLine _orderline,_order.OrderId)。

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. 您的ItemRrelated应该实现一些带有parentId属性的接口。 Then you can just add it to DbSet if it's not exists already. 然后,可以将其添加到DbSet(如果尚不存在)。

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. 在编写通用方法之前, 必须先定义交互作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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