1

我有一个保存按钮,它将在我的“IncidentActions”表中添加新数据,同时更新我的​​“事件”表中的 3 列。

我该怎么做

这是我用来尝试完成此任务的 C# 代码。

protected void cmdSave_Click(object sender, EventArgs e)
    {
        Context db = new Context();

        // This will check the Action from the actions table and 
        var actionID = (from i in db.Actions
                       where i.Actions == tbActionType.Text
                       select i.ActionsID).First();

        long ID = Convert.ToInt64(Request.QueryString["IncidentID"]);


        // TODO: update the incident table accordingly
        IncidentAction act = new IncidentAction
        {
            IncidentID = ID,
            ActionDate = (DateTime)dpActionDate.SelectedDate,
            ActionsID = Convert.ToInt32(actionID),
            StatusID = statID,
            IsPublic = false,
            Title = tbTitle.Text.Trim(),
            PeriodValue = Convert.ToInt64(txtDuration.Text),
            Description = txtDescription.Text.Trim(),
            EstimatedCost = txtEstimatedCost.Text == string.Empty ? (decimal?)null : Convert.ToDecimal(txtEstimatedCost.Text),
            ActualCost = txtActualCost.Text == string.Empty ? (decimal?)null : Convert.ToDecimal(txtActualCost.Text),
            LastUpdated = DateTime.Now,
            UpdatedBy = Convert.ToString(loggedInUserName),
            CreatedByUserID = Convert.ToInt32(loggedInUserID),
            Active = true
        };


                db.IncidentActions.Add(act);
                db.SaveChanges();

                Incident inc = new Incident
                {
                    IncidentID = ID,
                    StatusID = statID_new,
                    IncidentPendingDate = DateTime.Now
                };
                db.SaveChanges();
            }

    }
4

2 回答 2

0

我确实解决了谢谢

我改变了这段代码

db.IncidentActions.Add(act);
            db.SaveChanges();

            Incident inc = new Incident
            {
                IncidentID = ID,
                StatusID = statID_new,
                IncidentPendingDate = DateTime.Now
            };
            db.SaveChanges();

对此

// add to the IncidentAction table.
                db.IncidentActions.Add(act);
                db.SaveChanges();

                // Update the Incident Table.
                Incident inc = (from i in db.Incidents
                                where i.IncidentID == ID
                                select i).First();

                inc.StatusID = statID_new,
                inc.IncidentPendingDate = DateTime.Now;
                db.SaveChanges();
于 2013-06-06T07:47:19.413 回答
0

一个问题似乎是您没有Incident从数据库中引用正确的记录。您可以先查询数据库以获取要更新的记录。

如果您想在“同一时间”(即在同一事务中)同时执行它们,请删除第一个db.SaveChanges(). 然后第二个将一起进行插入和更新。

此外,DateTime.Now每次调用时都可以返回不同的值。如果您希望时间戳在每条记录上匹配,只需调用DateTime.Now一次,将结果保存到变量中,然后在两个地方都使用它。

像这样的东西:

Context db = new Context();
var now = DateTime.Now;

IncidentAction act = new IncidentAction
{
    IncidentID = ID,
    ...
    LastUpdated = DateTime.Now,
};


db.IncidentActions.Add(act);

Incident inc = db.Incidents.First(i => i.IncidentID == ID);
inc.StatusID = statID_newl
inc.IncidentPendingDate = now;

db.SaveChanges();
于 2013-06-05T14:38:12.223 回答