Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of linq cascade deletion

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the method of linq cascade deletion". In the daily operation, I believe that many people have doubts about the method of linq cascade deletion. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of linq cascade deletion?" Next, please follow the editor to study!

Cascading deletion (cascading deletes) also deletes rows in child tables when you delete foreign keys from the parent table. If some rows are deleted on the primary key column of a table, the same information will be deleted on any foreign key column of the related table if cascading deletion is enabled.

/ cascading deletions (rows in child tables are also deleted when foreign keys are deleted from the parent table) / public static void CascadingDeletes (T TEntity) where T: class / / where T: INotifyPropertyChanging, INotifyPropertyChanged {var _ type = TEntity.GetType (); var _ prop = _ type.GetProperties () / / find whether there is an attribute with "AssociationAttribute" tag (the attribute with "AssociationAttribute" tag in Linq represents the appearance) var _ assolist = _ prop.Where (c = > c.GetCustomAttributes (true) .Any (a = > ((Attribute) a). GetType (). Name = = "AssociationAttribute") & c.PropertyType.IsGenericType) / / this attribute must be generic / / other records with foreign key associations if (_ assolist.Count () > 0) {foreach (var _ asso in _ assolist) {var _ items = _ asso.GetValue (TEntity, null); var _ tItems = _ items.GetType (); var _ item = _ tItems.GetProperty ("Item"); var _ tItem = _ item.PropertyType; var _ countProp = _ tItems.GetProperty ("Count") / / get the attribute "Count" if (_ countProp = = null) throw new Exception ("Cannot find the property 'Count' in" + _ items.ToString ()) in the generic type; var _ count = (int) _ countProp.GetValue (_ items, null); / / get the number of associated records for (int I = 0; I < _ count; irecords +) {var _ value = _ item.GetValue (_ items, new object [] {I}); CascadingDeletes (_ value) / / delete records with foreign keys associated with other tables} try {DeleteByName (TEntity); / / delete records without foreign key associations} catch (Exception ex) {throw ex;}}

I would like to introduce to you in a more popular language when cascading deletion: for example, a data in the employee table is the department ID, and the department ID is the primary key of the department table. If it is cascaded deletion, when department An is deleted, all employees belonging to department A will be deleted. Linq cascading deletions are more difficult to implement because of the complexity of the Linq language. Let's continue with the above code to implement the Linq cascading deletion.

Public static void DeleteByName (T TEntity) where T: class {var type = TEntity.GetType (); var table = TableFactory.CreateTable (type); table.DeleteOnSubmit (TEntity); / / Console.WriteLine ("Delete:" + TEntity.ToString ());} public static class TableFactory {public static System.Data.Linq.Table CreateTable () where T: class {return Database.NWDB.GetTable ();} public static System.Data.Linq.ITable CreateTable (Type t) {return Database.NWDB.GetTable (t) }} public static class Database {private static DLinq.NWDBDataContext _ NWDB = null; public static DLinq.NWDBDataContext NWDB {get {if (_ NWDB = = null) _ NWDB = new DLinq.NWDBDataContext (); return _ NWDB;} at this point, the study of "what is the method of linq cascade deletion" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report