Funny Linq Part4:通用的级联删除

Funny Linq Part4:通用的级联删除

         级联删除(cascading deletes)
从父表中删除外键时也会删除子表中的行。如果在一个表的主键列上删除了一些行,那么在启用级联删除的情况下,将在相关表的任何外键列上删除相同信息。
  1.         /// <summary>
  2.         /// 级联删除(从父表中删除外键时也会删除子表中的行)
  3.         /// </summary>
  4.         /// <typeparam name="T"></typeparam>
  5.         /// <param name="TEntity"></param>
  6.         /// <returns></returns>
  7.         public static void CascadingDeletes<T>(T TEntity) where T : class // where T : INotifyPropertyChanging, INotifyPropertyChanged
  8.         {
  9.             var _type = TEntity.GetType();
  10.             var _prop = _type.GetProperties();
  11.             //查找是否有“AssociationAttribute”标记的属性(Linq中有“AssociationAttribute”标记的属性代表外表)
  12.             var _assolist = _prop.Where(
  13.                 c => c.GetCustomAttributes(true).Any(
  14.                     a => ((Attribute)a).GetType().Name == "AssociationAttribute") &
  15.                     c.PropertyType.IsGenericType);//该属性必需是泛型
  16.             //其他表有外键关联的记录
  17.             if (_assolist.Count() > 0)
  18.             {
  19.                 foreach (var _asso in _assolist)
  20.                 {
  21.                     var _items = _asso.GetValue(TEntity, null);
  22.                     var _tItems = _items.GetType();
  23.                     var _item = _tItems.GetProperty("Item");
  24.                     var _tItem = _item.PropertyType;
  25.                     var _countProp = _tItems.GetProperty("Count");//获取泛型中的属性“Count”
  26.                     if (_countProp == null)
  27.                         throw new Exception("Cannot find the property 'Count' in " + _items.ToString());
  28.                     var _count = (int)_countProp.GetValue(_items, null);//获取关联记录的数量
  29.                     for (int i = 0; i < _count; i++)
  30.                     {
  31.                         var _value = _item.GetValue(_items, new object[] { i });
  32.                         CascadingDeletes(_value);//删除其他表有外键关联的记录                        
  33.                     }
  34.                 }
  35.             }
  36.             try
  37.             {                
  38.                 DeleteByName(TEntity);//删除没外键关联的记录
  39.             }
  40.             catch (Exception ex)
  41.             {
  42.                 throw ex;
  43.             }
  44.         }
  1. public static void DeleteByName<T>(T TEntity) where T : class
  2.         {
  3.             var type = TEntity.GetType();
  4.             var table = TableFactory.CreateTable(type );            
  5.             table.DeleteOnSubmit(TEntity);
  6.             //Console.WriteLine("Delete:"+ TEntity.ToString());
  7.         }
  1.      public static  class TableFactory
  2.      { 
  3.         public static System.Data.Linq.Table<T> CreateTable<T>() where T : class
  4.         {
  5.             return Database.NWDB.GetTable<T>();
  6.         }
  7.         public static System.Data.Linq.ITable  CreateTable (Type t) 
  8.         {
  9.             return Database.NWDB.GetTable(t);
  10.         }
  11.     }
  12.     public   static class Database
  13.     {
  14.         private static DLinq.NWDBDataContext _NWDB = null;
  15.         public static DLinq.NWDBDataContext NWDB
  16.         {
  17.             get
  18.             {
  19.                 if (_NWDB == null)
  20.                     _NWDB = new DLinq.NWDBDataContext();
  21.                 return _NWDB;
  22.             }
  23.         }
  24.         
  25.     }
    注: DLinq.NWDBDataContext 就是对应的dbml文件的类名。
    希望这篇文章能给学习Linq的人带来帮助,也随便学学Reflection~
   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值