. NET open source database design tool MR.E for LINQ (EF 6.1) tutorial (ii) cascading deletions and triggers

Source: Internet
Author: User

1. Establishing a cascade Delete

MR.E Cascade Delete is not a database with that cascade Delete, but MR.E comes with it, so it can trigger your C # written trigger.

First, establish a cascade delete relationship, such as two tables, userinfo and UserDocument,

The UserDocument table relies on the UserID field, which is associated with userinfo. Now I want to implement, when the data inside the UserInfo deleted, automatically delete the UserDocument table inside the userid=userinfo.id of those data, what should be done?

First, double-click UserInfo to open its property editor, click the Cascade Delete setting item, add a cascade delete relationship

Then compile the database DLL, we go to the code there to experiment.

2. Triggers

First create a new Userdocumentaction class, Inherit Entitydb.actioncapture<test.userdocument>, as a trigger for the UserDocument table

usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacelinqtest1{ Public classUserdocumentaction:entitydb.actioncapture<test.userdocument>    {         Public Override voidBeforeInsert (Objectdatabase, Entitydb.databasemodifyeventarg e) {Debug.WriteLine ("userdocument BeforeInsert"); }         Public Override voidAfterInsert (Objectdatabase, Entitydb.databasemodifyeventarg e) {            vardata =(test.userdocument) E.dataitem; Debug.WriteLine (string. Format ("UserDocument found new data, id={0} filename={1}", Data.id, data.        FileName)); }         Public Override voidBeforeUpdate (Objectdatabase, Entitydb.databasemodifyeventarg e) {                    }         Public Override voidAfterUpdate (Objectdatabase, Entitydb.databasemodifyeventarg e) {                    }         Public Override voidBeforeDelete (Objectdatabase, Entitydb.databasemodifyeventarg e) {            vardb =(Test.DB.TestDB) database; vardata =(test.userdocument) E.dataitem; //data only has a value in the ID field, so to get all the values out of the field, you need to go to the database and fetch it once .data = db. Userdocument.firstordefault (M=>m.id = =data.id); Debug.WriteLine ("data is about to be deleted: Filename="+ data. FileName +"desc="+data.        DESC); }         Public Override voidAfterDelete (Objectdatabase, Entitydb.databasemodifyeventarg e) {Debug.WriteLine ("userdocument AfterDelete"); }    }}

Then write the code to insert the data, delete the data, and see if Userdocumentaction can capture the event correctly

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacelinqtest1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidForm1_Load (Objectsender, EventArgs e) {            using(vardb =NewTest.DB.TestDB (@"data source= "" F:\SqliteLinqTest\TestDB """, EntityDB.DatabaseType.Sqlite)) {                //Start a transactiondb.                BeginTransaction (); Try                {                    //add userinfo table Data                    varuser =NewTest.userinfo (); User. UserName="Zhang San"; User. Password="123"; Db.                    Update (user); //add userdocument table Data                    varUserdoc =Newtest.userdocument (); Userdoc.userid=user.id; Userdoc.filename="d:\\ test document. doc"; Userdoc.desc="Test Document"; Db.                    Update (Userdoc); //Delete Userdb.                    Delete (user); //Commit a transactiondb.committransaction (); }                Catch                {                    //rolling back a transactiondb.                    RollbackTransaction (); Throw; }            }        }    }}

Run the code and discover the db. Delete (user); When the user's data is deleted, the cascade delete works, automatically deletes the data inside the userdocument , and is captured by the Userdocumentaction trigger.

. NET open source database design tool MR.E for LINQ (EF 6.1) tutorial (ii) cascading deletions and triggers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.