如何在1表中插入数据并使用linq更新sql数据库中的另一个表

时间:2013-06-05 14:31:11

标签: c# asp.net sql linq

我有一个保存按钮,可以在我的“事件行为”中添加新数据。表格同时更新了我的“事件”中的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();
            }

    }

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();