linq to sql c#:SubmitChanges()不更新数据库

linq to sql c#:SubmitChanges()不更新数据库,c#,linq-to-sql,C#,Linq To Sql,下面更新数据库的方法称为UpdateHold()。对象的此实例使用第二个构造函数。我运行了debug来验证hold对象“thishhold”是否正在更新表单中正在更改的值。但是,当我在提交之后查看db对象的值时,我看到它没有改变,在物理数据库中也是如此。为什么不更新 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing

下面更新数据库的方法称为UpdateHold()。对象的此实例使用第二个构造函数。我运行了debug来验证hold对象“thishhold”是否正在更新表单中正在更改的值。但是,当我在提交之后查看db对象的值时,我看到它没有改变,在物理数据库中也是如此。为什么不更新

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Windows.Forms;

namespace Coke_Hold_Database
{
    public partial class frmFGHold : Form
    {
        private PINAuthentication cert = new PINAuthentication();
        linqCokeDBDataContext db = new linqCokeDBDataContext();
        Record_HoldData thisHold;
        bool isNew; //flag if hold is new or not


        public frmFGHold()
        {
            //constructor for new hold
            InitializeComponent();
            populateLists();
            thisHold = new Record_HoldData();

            isNew = true;
            btnEdit.Visible = false;
        }

        public frmFGHold(Record_HoldData holdData)
        {
            //constructor for existing hold (edit)
            InitializeComponent();
            populateLists();
            this.thisHold = holdData;

            //fill out the form
            FillForm();

            isNew = false;
            btnEdit.Visible = true;
        }

        private void FillForm()
        {
            //fill out the form with existing information

            txtPONumber.Text = thisHold.PO_.ToString();
            cboProductionSupervisor.SelectedValue = thisHold.ProductionSupervisor;
            cboLineNumber.SelectedValue = thisHold.LineNumber;
            cboQASupervisor.SelectedValue = thisHold.QASupervisor;
            cboFlavorName.SelectedValue = thisHold.Flavor;
            cboContainer.Text = thisHold.ContainerType;
            cboContainerSize.Text = thisHold.ProductSize;
            cboPackage.Text = thisHold.Package;
            txtQuantity.Text = thisHold.HoldQty.ToString();
            mtbDateCode.Text = thisHold.DateCode;
            cboDefectiveComponent.Text = thisHold.NonConformingItem;
            cboDefectReason.Text = thisHold.NonConformance;
            cboOccuredAt.Text = thisHold.FoundDuring;
            chkTestRequired.Checked = (bool) thisHold.TestRequired;
            txaDescription.Text = thisHold.Comments;
            txaRootCauseAnalysis.Text = thisHold.RootCause;
        }

        private void UpdateHoldObject()
        {
            thisHold.PO_ = int.Parse(txtPONumber.Text);
            thisHold.ProductionSupervisor = (int)cboProductionSupervisor.SelectedValue;
            thisHold.LineNumber = (int) cboLineNumber.SelectedValue;
            thisHold.QASupervisor = (int) cboQASupervisor.SelectedValue;
            thisHold.Flavor = (int) cboFlavorName.SelectedValue;
            thisHold.ContainerType = cboContainer.Text;
            thisHold.ProductSize = cboContainerSize.Text;
            thisHold.Package = cboPackage.Text;
            thisHold.HoldQty = int.Parse(txtQuantity.Text);
            thisHold.DateCode = mtbDateCode.Text;
            thisHold.NonConformingItem = cboDefectiveComponent.Text;
            thisHold.NonConformance = cboDefectReason.Text;
            thisHold.FoundDuring = cboOccuredAt.Text;
            thisHold.TestRequired = chkTestRequired.Checked;
            thisHold.Comments = txaDescription.Text;
            thisHold.RootCause = txaRootCauseAnalysis.Text;
        }

        private void CreateNewHold()
        {
            db.Record_HoldDatas.InsertOnSubmit(thisHold);
            //GenerateHoldNumber()
            db.SubmitChanges();
            MessageBox.Show(this, "Hold submitted!\n\nYou're hold number is " + thisHold.HoldID.ToString(),"Hold #" + thisHold.HoldID.ToString() + " created", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }

        private void UpdateHold()
        {
            db.SubmitChanges();
            MessageBox.Show(this, "Hold #" + thisHold.HoldID.ToString() + " updated.", thisHold.HoldID.ToString() + " updated", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();   
        }

        private bool ValidateEntry()
        {
            //TODO write some frakkin validation
            return true;
        }

        private void GetPIN()
        {
            frmPINPrompt pin = new frmPINPrompt(cert);
            pin.ShowDialog();
        }

        private void SubmitData()
        {
            UpdateHoldObject();
            GetPIN();

            if (ValidateEntry() && cert.authenticated)
            {
                try
                {
                    if (isNew)
                    {
                        thisHold.LabTech = cert.empNumber;
                        thisHold.LastEditBy = cert.empNumber;
                        thisHold.HoldStatus = "Open";
                        thisHold.DateOpened = DateTime.Now;
                        thisHold.LastEditDate = DateTime.Now;

                        CreateNewHold();
                    }
                    else
                    {
                        thisHold.LastEditBy = cert.empNumber;
                        thisHold.LastEditDate = DateTime.Now;

                        UpdateHold();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        public void populateLists()
        {
            //load production names
            var productionNames =
                from a in db.LUT_Employees
                where a.position == "Supervisor" && a.department == "Production"
                select new { ID = a.ID, Names = a.lastName + ", " + a.firstName };

            cboProductionSupervisor.DataSource = productionNames;
            cboProductionSupervisor.DisplayMember = "Names";
            cboProductionSupervisor.ValueMember = "ID";

            //load QA names
            var qaNames =
                from a in db.LUT_Employees
                where a.position == "Supervisor" && a.department == "Quality Assurance"
                select new { ID = a.ID, Names = a.lastName + ", " + a.firstName };

            cboQASupervisor.DataSource = qaNames;
            cboQASupervisor.DisplayMember = "Names";
            cboQASupervisor.ValueMember = "ID";

            //load flavor names
            var flavorNames =
                from a in db.LUT_Flavors
                select new { ID = a.ID, Flavor = a.flavor };

            cboFlavorName.DataSource = flavorNames;
            cboFlavorName.DisplayMember = "flavor";
            cboFlavorName.ValueMember = "ID";

            //load line numbers
            var lineNumbers =
                (from a in db.LUT_ProductionLines
                select new { a.lineNumber }).ToList();

            cboLineNumber.DataSource = lineNumbers; 
            cboLineNumber.DisplayMember = "LineNumber";
            cboLineNumber.ValueMember = "LineNumber";
        }

        private void mtbDateCode_MouseUp(object sender, MouseEventArgs e)
        {
            SendKeys.Send("{HOME}");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to cancel? All data will be lost!", "Confirm Cancel", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (result == DialogResult.Yes)
            {
                this.Close();
            }
            else { }
        }





        private void btnSubmit_Click(object sender, EventArgs e)
        {
            SubmitData();

        }

        private void linkAutoFill_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            //autogenerage date code
            mtbDateCode.Text = Etc.BuildDateCode(DateTime.Now,(int)cboFlavorName.SelectedValue,cboContainer.Text,Etc.getDayCode(DateTime.Now),int.Parse(cboLineNumber.Text));
        }

        private void cboContainerSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            //load package sizes
            var packageSizes =
                from a in db.LUT_Packagings
                where a.Container == cboContainer.Text && a.Size == cboContainerSize.SelectedValue
                select new { ID = a.ID, Size = a.Package };

            cboPackage.DataSource = packageSizes;
            cboPackage.DisplayMember = "Size";
            cboPackage.ValueMember = "ID";

        }

        private void cboContainer_SelectedIndexChanged(object sender, EventArgs e)
        {
            //make container size list
            var containerSizes =
                from a in db.LUT_Containers
                where a.ContainerType == cboContainer.Text
                select new { ID = a.ID, Size = a.size };

            cboContainerSize.DataSource = containerSizes;
            cboContainerSize.DisplayMember = "Size";
            cboContainerSize.ValueMember = "ID";

            //load components
            var defectiveComponents =
                from a in db.LUT_ContainerComponents
                where a.Container == cboContainer.Text
                select new { ID = a.ID, Comp = a.Component };

            cboDefectiveComponent.DataSource = defectiveComponents;
            cboDefectiveComponent.DisplayMember = "Comp";
            cboDefectiveComponent.ValueMember = "ID";
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //TODO ask verify PIN
            foreach (Control c in this.Controls)
            {
                c.Enabled = true;
            }

            btnSubmit.Text = "Update Hold";
        }
    }
}

数据库没有更新,因为您正在提交对
db
DataContext的更改,并且您通过构造函数提供的“保持”没有附加到该DataContext。因此,
db
DataContext无法跟踪您对对象所做的更改


您可以尝试使用
DataContext.Attach
,或者更好的是,使用加载对象的原始数据上下文提供表单。

您是尝试更新单行还是批量更新??单行。我通常对submitall没有问题您对在类的全局范围内初始化数据上下文,而不是在每个需要使用数据上下文的方法中使用
use
块有什么看法?@Nick,这个主意不好,仍然不能解决您的问题,因为这样每个类都有自己的数据上下文。您使用的
公式也让我感到害怕,因为这会创建更多的数据上下文。最好的解决方案是确定“工作单元”(即更新对象)并为该工作单元创建一个数据上下文,然后将所述数据上下文传递给需要它的类和方法。您可能需要研究依赖注入。我指的是您描述的工作单元。得到第二个意见很好。谢谢你的建议。宾果。我知道这和数据上下文有关。考虑到它如何知道该用什么,我的脑子里并没有想起来。还是有点新鲜,想弄明白。谢谢