. Net open-source database design tool Mr. E For Linq (EF 6.1) Tutorial (ii) cascading deletion and triggers, mr. elinq
1. Establish cascading Deletion
The cascade deletion of Mr. E is not the cascade deletion that comes with the database, but the one that comes with Mr. E, so it can trigger the trigger written in C.
First, establish a cascading deletion relationship. For example, there are two tables: UserInfo and UserDocument,
The UserDocument table is associated with UserInfo Based on The UserID field. Now I want to implement this. When the data in UserInfo is deleted, what should I do to automatically delete the data in UserID = UserInfo. id in the UserDocument table?
First, double-click UserInfo to open its Attribute Editor. Click the cascade deletion setting to add a cascade deletion relationship.
Then compile the database dll. Let's experiment with the code.
2. triggers
Create a UserDocumentAction class that inherits EntityDB. ActionCapture <Test. UserDocument> as a trigger for the UserDocument table.
Using System; using System. collections. generic; using System. diagnostics; using System. linq; using System. text; using System. threading. tasks; namespace LinqTest1 {public class UserDocumentAction: EntityDB. actionCapture <Test. userDocument> {public override void BeforeInsert (object database, EntityDB. databaseModifyEventArg e) {Debug. writeLine ("UserDocument BeforeInsert");} public override void AfterInsert (object database, EntityDB. databaseModifyEventArg e) {var data = (Test. userDocument) e. dataItem; Debug. writeLine (string. format ("UserDocument found new data, id = {0} FileName = {1}", data. id, data. fileName);} public override void BeforeUpdate (object database, EntityDB. databaseModifyEventArg e) {} public override void AfterUpdate (object database, EntityDB. databaseModifyEventArg e) {} public override void BeforeDelete (object database, EntityDB. databaseModifyEventArg e) {var db = (Test. DB. testDB) database; var data = (Test. userDocument) e. dataItem; // data only has a value for the id field. Therefore, to obtain the values of all fields, you must go to the database to retrieve data = db. userDocument. firstOrDefault (m => m. id = data. id); Debug. writeLine ("data to be deleted: FileName =" + data. fileName + "Desc =" + data. desc);} public override void AfterDelete (object database, EntityDB. databaseModifyEventArg e) {Debug. writeLine ("UserDocument AfterDelete ");}}}
Register the trigger at the application entry.
/// <Summary> /// main entry point of the application. /// </Summary> [STAThread] static void Main () {EntityDB. DBContext. AddActionCapture (new UserDocumentAction (); Application. Run (new Form1 ());}
Write the code for inserting data and deleting the data to see if UserDocumentAction can correctly capture the event.
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. diagnostics; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace LinqTest1 {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void Form1_Load (object sender, EventArgs e) {using (var db = new Test. DB. testDB (@ "data source =" "F: \ SqliteLinqTest \ TestDB" ", EntityDB. databaseType. sqlite) {// start transaction db. beginTransaction (); try {// Add UserInfo table data var user = new Test. userInfo (); user. userName = "zhangsan"; user. password = "123"; db. update (user); // Add UserDocument table data var userDoc = new Test. userDocument (); userDoc. userID = user. id; userDoc. fileName = "d :\\ test document .doc"; userDoc. desc = "test document"; db. update (userDoc); // delete user db. delete (user); // submit the transaction database. commitTransaction ();} catch {// roll back the transaction db. rollbackTransaction (); throw ;}}}}}
Run the code to find the database. delete (user); When deleting user data, cascading deletion takes effect. The data in UserDocument is automatically deleted and captured by the trigger UserDocumentAction.