简体   繁体   English

从LINQ To Sql数据上下文中获取插入和更新SQL查询

[英]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. 我有一个LINQ To Sql数据上下文,具有到表User和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. 因此,我想获得针对特定实体通过数据上下文为插入/更新生成的相应SQL。

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) 我知道context.Log属性可用于捕获生成的整个SQL,这种方法的问题是,它将捕获所有与我不相关的SQL(例如某些其他实体的更改脚本)

 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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