简体   繁体   中英

Get the insert and update SQL queries from LINQ To Sql data context

I have a LINQ To Sql data context, with a mapping to tables User and Group. A user belongs to a Group.

So I would like to get the corresponding SQL generated for Insert/Update by data context against a particular entity.

For eg

  using (var context = new TestBedDataContext())
        {
            using (var trans = new TransactionScope())
            {
                context.Users.InsertOnSubmit(new User
                    {
                        Name = "Test",
                        Password = "Password",
                        Username = "test",
                        Group1 = new Group
                        {
                            Name = "Group1"
                        }
                    });

                // Get the query for User entity
            }
        }

Here I would like to get the queries that will be generated for inserting a new user along with Group entity.

I know context.Log property can be used to capture the entire SQL generated, the problem with that approach is, it will catch all the SQL which are not in my interests (like change scripts for some other entities)

 public void addPatientInformation() {
    using(DbClassesDataContext myDb = new DbClassesDataContext()) {
        myDb.PatientInfos.InsertOnSubmit(new PatientInfo {
            Phy_ID = physcianID,
            Pat_First_Name = txtFirstName.Text,
            Pat_Middle_Init = txtMiddleName.Text,
            Pat_Last_Name = txtLastName.Text,
            Pat_Gender = cmbGender.Text,
            Pat_Marital_Status = cmbMaritalStatus.Text,
            Pat_Date_Of_Birth = dtpDOB.Value,
            Pat_Home_Add = txtHomeAdd.Text,
            Pat_Home_Num = txtPhone.Text,
            Pat_Work_Add = txtWorkAdd.Text,
            Pat_Work_Num = txtWorkPhone.Text,
            Pat_Prim_Physician = txtPrimPhysician.Text,
            Pat_Ref_Physician = txtRefePhysician.Text,
        });
        myDb.SubmitChanges();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM