LINQ的范围身份(scope identity with linq)

2019-11-03 06:19发布

你喜欢的LINQ范围身份的程序要在TODO语句中使用它

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtZip.Text != "" && txtAdd1.Text != "" && txtCity.Text != "")
        {
            TestDataClassDataContext dc = new TestDataClassDataContext();
            Address addr = new Address()
            {
                AddressLine1 = txtAdd1.Text,
                AddressLine2 = txtAdd2.Text,
                City = txtCity.Text,
                PostalCode = txtZip.Text,
                StateProvinceID = Convert.ToInt32(ddlState.SelectedValue)
            };
            dc.Addresses.InsertOnSubmit(addr);
            lblSuccess.Visible = true;
            lblErrMsg.Visible = false;
            dc.SubmitChanges();
     //
     //    TODO: insert new row in EmployeeAddress to reference CurEmp to newly created address
     //
            SetAddrList();
        }
        else
        {
            lblErrMsg.Text = "Invalid Input";
            lblErrMsg.Visible = true;
        }
    }

    protected void SetAddrList()
    {
        TestDataClassDataContext dc = new TestDataClassDataContext();
        dc.ObjectTrackingEnabled = false;

        var addList = from addr in dc.Addresses
                      from eaddr in dc.EmployeeAddresses
                      where eaddr.EmployeeID == _curEmpID && addr.AddressID == eaddr.AddressID
                      select new
                      {
                          AddValue = addr.AddressID,
                          AddText = addr.AddressID,
                      };
        ddlAddList.DataSource = addList;
        ddlAddList.DataValueField = "AddValue";
        ddlAddList.DataTextField = "AddText";
        ddlAddList.DataBind();
        ddlAddList.Items.Add(new ListItem("<Add Address>", "-1"));
    }

Answer 1:

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtZip.Text != "" && txtAdd1.Text != "" && txtCity.Text != "")
        {
            TestDataClassDataContext dc = new TestDataClassDataContext();
            Address addr = new Address()
            {
                AddressLine1 = txtAdd1.Text,
                AddressLine2 = txtAdd2.Text,
                City = txtCity.Text,
                PostalCode = txtZip.Text,
                StateProvinceID = Convert.ToInt32(ddlState.SelectedValue)
            };
            dc.Addresses.InsertOnSubmit(addr);

            dc.SubmitChanges();
            int nAddID = addr.AddressID;
            EmployeeAddress empadd = new EmployeeAddress()
            {
                EmployeeID = Convert.ToInt32(_curEmpID),
                AddressID = nAddID
            };
            dc.EmployeeAddresses.InsertOnSubmit(empadd);
            dc.SubmitChanges();
            lblSuccess.Visible = true;
            lblErrMsg.Visible = false;
            SetAddrList();
        }
        else
        {
            lblErrMsg.Text = "Invalid Input";
            lblErrMsg.Visible = true;
        }
    }

OK已经想通了,我不知道,如果它是那么容易或不



Answer 2:

你可以把它作为参数传递给SetAddrList无效,那么你将有机会获得该对象包括新创建的主键的所有属性。

SetAddrList(addr);

...
...

protected void SetAddrList(Address addr)


文章来源: scope identity with linq