Linq to SQL级联删除与反射 [英] Linq to SQL cascading delete with reflection

查看:79
本文介绍了Linq to SQL级联删除与反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找将linq中的级联删除从c#级联到sql的通用解决方案,但是我找不到任何解决方案.

I was looking for a general solution for cascading delete in linq to sql from c#, but I could not find any yet.

所以我想出了自己的解决方案,该解决方案处理一对多关系.

So I came up with my own solution, which handles one to many relations.

这不是删除实体的一般方法,但是某些情况下需要这样做.因此,在进入生产环境之前,我想知道您对此有何想法?

It would not be the general way to delete entities, but some edge case requires it. So before it goes to the production environment, I would like to know that what are you think about it?

这似乎可行,但是在哪里会失败,它的利弊是什么?请注意,它仅应遍历关系树的下方.

It seems to work, but what are the pros and cons, where it can fail? Note that it only supposed to traverse the relation tree downside.

public class CascadingDeleteHelper
{
        public void Delete(object entity, Func<object, bool> beforeDeleteCallback)
        {
            if (!(entity is BusinessEntity))
            {
                throw new ArgumentException("Argument is not a valid BusinessEntity");
            }

            if (beforeDeleteCallback == null || beforeDeleteCallback(entity))
            {
                Type currentType = entity.GetType();
                foreach (var property in currentType.GetProperties())
                {
                    var attribute = property
                        .GetCustomAttributes(true)
                        .Where(a => a is AssociationAttribute)
                        .FirstOrDefault();

                    if (attribute != null)
                    {
                        AssociationAttribute assoc = attribute as AssociationAttribute;

                        if (!assoc.IsForeignKey)
                        {
                            var propertyValue = property.GetValue(entity, null);
                            if (propertyValue != null && propertyValue is IEnumerable)
                            {
                                IEnumerable relations = propertyValue as IEnumerable;
                                List<object> relatedEntities = new List<object>();

                                foreach (var relation in relations)
                                {
                                    relatedEntities.Add(relation);
                                }

                                relatedEntities.ForEach(e => Delete(e, beforeDeleteCallback));
                            }
                        }
                    }
                }

                SingletonDataContext.DataContext.GetTable(currentType).DeleteOnSubmit(entity);
                SingletonDataContext.DataContext.SubmitChanges();
            }
        }
    }

非常感谢您,

内格拉

推荐答案

解决方案的优点:

  • 很通用
  • 不需要在架构更新上更改数据库

缺点:

  • 容易出错(更多自定义代码)
  • 性能(很可能)不是最佳的

有一个不太复杂但需要大量维护的解决方案,它是将 ON CASCADE DELETE 规则添加到数据库中.

There is a less complex, but more maintenance heavy solution, which is adding ON CASCADE DELETE rules into your database.

这篇关于Linq to SQL级联删除与反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆