参考以下教程:ADO.NET Entity Framework Tutorial and Basics
我正在尝试使用WEB应用程序创建相同的代码。我可以获取信息,但是我的更新按钮事件不会保存更改。按下更新按钮时, CurrentPayroll 对象因某种原因为空。我选择了设置CurrentPayroll对象的不同作者。我尝试过使用会话,但这也行不通。
以下是代码:
PayrollView.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="<<" />
<asp:Button ID="btnPrevious" runat="server" Text="<" />
<asp:Button ID="btnNext" runat="server" Text=">" />
<asp:Button ID="btnLast" runat="server" Text=">>" />
</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>
PayrollView.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)
{
}
}
}
答案 0 :(得分:1)
管理以解决它。您需要分离Payroll对象。创建一个新的上下文,然后将存储的对象附加到新的上下文。
publishContext.Detach(CurrentPayroll);
以下是更新的活动:
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();
}