PostCreate can't see the data just inserted

PostCreate can't see the data just inserted

shuminliushuminliu Posts: 14Questions: 6Answers: 0
edited March 2021 in Editor

I set a function for postCreate, when the function is called, it seems the db operation is not finished:

("api/User")
public ActionResult ProcesstUsers()
        {
            var request = HttpContext.Request;
            var DbType = "sqlserver";
            var dbConnection = "......";

            using (var db = new Database(DbType, DbConnection))
            {
                var editor = new Editor(db, "User")
                    .Model<User>("User")
                    .Model<Department>("Department")
                    .Field(new Field("User.Username"))
                    .Field(new Field("User.DepartmentCode").Options(new Options()
                            .Table("Department")
                            .Value("DepartmentCode")
                            .Label(new[] { "DepartmentName" })
                       ))
                    .Field(new Field("Department.DepartmentName"))
                    .LeftJoin("Department", "User.DepartmentCode", "=", "Department.DepartmentCode");

                editor.PostCreate += (sender, e) => {
                    SendUserActivationEmail(int.Parse(e.Id.ToString()));
                };

                var response = editor.Process(request).Data();

                return Json(response);
            }

Inside of SendUserActivationEmail, I can see the ID of the newly inserted record, but it seems the record is not committed to the database yet and the User table is locked, so the read with ID went timeout. I'm wondering if there is a way for postCreate to be triggered after the DB operation is completed. Thanks!

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • shuminliushuminliu Posts: 14Questions: 6Answers: 0

    I also tried the WriteCreate handler but still no luck :(

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    It will be because Editor uses a transaction by default - add .Transaction(false) just before the .Process(...) call and it should work.

    Alternatively, you can access the transaction that Editor is using with the Editor.DbTransaction() method. Carry out your queries on that and it should work.

    Allan

This discussion has been closed.