代码之家  ›  专栏  ›  技术社区  ›  Thomas

ASP.NET实体框架更新数据

  •  1
  • Thomas  · 技术社区  · 14 年前

    参考以下教程: ADO.NET Entity Framework Tutorial and Basics

    我正在尝试使用Web应用程序创建相同的代码。我可以获取信息,但是我的更新按钮事件不保存更改。这个 当前工资单 由于某种原因,当按下更新按钮时,对象为空。我会选择不同的作者来设置当前的工资单对象。我试过使用会话,但也不起作用。

    代码如下:

    工资滚动视图.aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="PayrollView.aspx.cs" Inherits="SodiumHydroxide.Public.PayrollView" %>
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
        }
    </style>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table class="style1">
        <tr>
            <td class="style2">
                Author
            </td>
            <td>
                <asp:DropDownList ID="ddAuthor" runat="server" OnSelectedIndexChanged="ddAuthor_SelectedIndexChanged"
                    AutoPostBack="true" ViewStateMode="Enabled">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style2">
                PayrollID
            </td>
            <td>
                <asp:Label ID="lblPayRollID" runat="server" Text="000"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="style2">
                Salary
            </td>
            <td>
                <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style2" colspan="2">
                <asp:Button ID="btnFirst" runat="server" Text="&lt;&lt;" />
                <asp:Button ID="btnPrevious" runat="server" Text="&lt;" />
                <asp:Button ID="btnNext" runat="server" Text="&gt;" />
                <asp:Button ID="btnLast" runat="server" Text="&gt;&gt;" />
            </td>
        </tr>
        <tr>
            <td class="style2" colspan="2">
                <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
                <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
                <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
                <asp:Label ID="lblFeedback" runat="server" ForeColor="Red"></asp:Label>
            </td>
        </tr>
    </table>
    </asp:Content>
    

    工资滚动视图.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.Objects;
    
    namespace SodiumHydroxide.Public
    {
        public partial class PayrollView : System.Web.UI.Page
        {
        PublishingCompanyEntities publishContext;
        Payroll CurrentPayroll;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            publishContext = new PublishingCompanyEntities();
    
            if (!Page.IsPostBack)
            {
                try
                {
                    this.ddAuthor.DataSource = publishContext.Author;
                    this.ddAuthor.DataTextField = "FirstName";
                    this.ddAuthor.DataValueField = "AuthorID";
                    this.ddAuthor.DataBind();
                }
                catch (ObjectDisposedException)
                {
                }
            } // if (!Page.IsPostBack)
        }
    
        protected void ddAuthor_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Selected = 0;
    
            try
            {
                Selected = Convert.ToInt32(this.ddAuthor.SelectedItem.Value);
            }
            catch (InvalidCastException) { }
    
            if (Selected > 0)
            {
                Author Authors = new Author();
                Authors.AuthorID = Selected;
    
                //Uses Linq-to-Entities
                IQueryable<Payroll> payrollQuery =
                   from p in publishContext.Payroll
                   where p.Author.AuthorID == Authors.AuthorID
                   select p;
                List<Payroll> SelectedPayroll = payrollQuery.ToList();
    
                if (SelectedPayroll != null && SelectedPayroll.Count > 0)
                {
                    CurrentPayroll = SelectedPayroll.First();
    
                    Session["CurrentPayroll"] = CurrentPayroll;
                }
                else
                {
                    CurrentPayroll = null;
    
                    Session["CurrentPayroll"] = CurrentPayroll;
                }
            }
    
            PopulateFields();
    
            this.lblFeedback.Text = "ddAuthor_SelectedIndexChanged " + Selected.ToString();
        }
    
        private void PopulateFields()
        {
            if (CurrentPayroll != null)
            {
                this.lblPayRollID.Text = CurrentPayroll.PayrollID.ToString();
                this.txtSalary.Text = CurrentPayroll.Salary.ToString();
                this.btnAdd.Enabled = false;
                this.btnDelete.Enabled = true;
                this.btnUpdate.Enabled = true;
            }
            else
            {
                this.lblPayRollID.Text = "Not on payroll";
                this.txtSalary.Text = "0";
                this.btnAdd.Enabled = true;
                this.btnDelete.Enabled = false;
                this.btnUpdate.Enabled = false;
            }
        }
    
        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
        }
    
        protected void btnAdd_Click(object sender, EventArgs e)
        {
    
        }
    
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            // Payroll UpdatePayroll = (Payroll)Session["CurrentPayroll"];
    
            CurrentPayroll.Salary = Convert.ToInt32(this.txtSalary.Text);
    
            publishContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
        }
    
        protected void btnDelete_Click(object sender, EventArgs e)
        {
    
        }
    }
    

    }

    1 回复  |  直到 14 年前
        1
  •  1
  •   Thomas    14 年前

    设法解决了问题。您需要分离工资单对象。创建新上下文,然后将存储的对象附加到新上下文。

    publishContext.detach(当前工资单);

    以下是更新的事件:

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Payroll StoredPayroll = (Payroll)Session["CurrentPayroll"];
    
            PublishingCompanyEntities UpdateContext = new PublishingCompanyEntities();
            UpdateContext.Attach(StoredPayroll);
    
            StoredPayroll.Salary = Convert.ToInt32(this.txtSalary.Text);
    
            int AffectedRows = UpdateContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
    
            this.lblFeedback.Text = "Affected Rows: " + AffectedRows.ToString();
        }