linq更新:“未找到或更改行”。例外

时间:2011-03-19 11:29:23

标签: c# asp.net linq

当我尝试更新或删除单个选定项目时,会抛出“未找到或更改行”。例外。基本上我只做了一个linq查询并使用了大部分的自动生成功能。我添加了LinqDataSource1_ContextCreated函数,但它没有帮助。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
               AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
               DataSourceID="LinqDataSource1" Width="100%" 
               onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="JobId" HeaderText="JobId" ReadOnly="True" />
            <asp:BoundField DataField="JobTitle" HeaderText="JobTitle" />
            <asp:BoundField DataField="Summary" HeaderText="Summary"/>
            <asp:BoundField DataField="Detail" HeaderText="Detail" />
            <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
            <asp:BoundField DataField="CompanyEmail" HeaderText="CompanyEmail" />
            <asp:CheckBoxField DataField="IsTop" HeaderText="IsTop"   />
            <asp:CheckBoxField DataField="IsShown" HeaderText="IsShown" />
              <asp:BoundField DataField="PostDate" HeaderText="PostDate" 
                ApplyFormatInEditMode="True" DataFormatString="{0:MMM,dd,yy}" 
                HtmlEncode="False" ReadOnly="True"/>
        <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True"/>

    </Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
           ContextTypeName="JobPostDataContext" EntityTypeName="" 
           onselecting="LinqDataSource1_Selecting" TableName="JobLists" 
           EnableDelete="True" EnableUpdate="True" >
            </asp:LinqDataSource>

  protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        JobPostDataContext db = new JobPostDataContext();

        var query = from uM in db.aspnet_Memberships
                    join uD in db.UserDetails
                    on uM.UserId equals uD.UserId
                    join u in db.aspnet_Users
                    on uM.UserId equals u.UserId
                    join jL in db.JobLists
                    on uM.UserId equals jL.UserId
                    where u.UserName == Request.QueryString["UserName"]

                    select new
                    {
                        jL.JobId,
                        jL.JobTitle,
                        jL.Summary,
                        jL.Detail,
                        jL.CompanyName,
                        jL.CompanyEmail,
                        jL.PostDate,
                        jL.IsTop,
                        jL.IsShown,
                        u.UserName
                    };

         e.Result = query;
    }

    protected void LinqDataSource1_ContextCreated(object sender, LinqDataSourceStatusEventArgs e)
    {

        JobPostDataContext db = e.Result as JobPostDataContext;

        System.Data.Linq.DataLoadOptions dl = new System.Data.Linq.DataLoadOptions();

        dl.LoadWith<JobList>(d => d.JobId);

        db.LoadOptions = dl;

    }  

未找到或更改行。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.Linq.ChangeConflictException:未找到或更改行。

来源错误:

在执行当前Web请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息。

堆栈追踪:

  

[ChangeConflictException:不行   发现或改变。]
  System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode   failureMode)+996823
  System.Data.Linq.DataContext.SubmitChanges(ConflictMode   failureMode)+378
  System.Data.Linq.DataContext.SubmitChanges()   +23 System.Web.UI.WebControls.LinqToSqlWrapper.SubmitChanges(DataContext   dataContext)+9
  System.Web.UI.WebControls.LinqDataSourceView.UpdateDataObject(对象   dataContext,Object table,Object   oldDataObject,Object newDataObject)   +115 System.Web.UI.WebControls.LinqDataSourceView.UpdateObject(Object   oldEntity,Object newEntity)+262
  System.Web.UI.WebControls.QueryableDataSourceView.ExecuteUpdate(IDictionary的   键,IDictionary值,IDictionary   oldValues)+115
  System.Web.UI.WebControls.ContextDataSourceView.ExecuteUpdate(IDictionary的   键,IDictionary值,IDictionary   oldValues)+100
  System.Web.UI.WebControls.LinqDataSourceView.ExecuteUpdate(IDictionary的   键,IDictionary值,IDictionary   oldValues)+41
  System.Web.UI.DataSourceView.Update(IDictionary的   键,IDictionary值,IDictionary   oldValues,   DataSourceViewOperationCallback   回调)+95
  System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow   row,Int32 rowIndex,Boolean   causeValidation)+1226
  System.Web.UI.WebControls.GridView.HandleEvent(EventArgs的   e,Boolean causeValidation,String   validationGroup)+716
  System.Web.UI.WebControls.GridView.OnBubbleEvent(对象   来源,EventArgs e)+95
  System.Web.UI.Control.RaiseBubbleEvent(对象   来源,EventArgs args)+37
  System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(对象   来源,EventArgs e)+121
  System.Web.UI.Control.RaiseBubbleEvent(对象   来源,EventArgs args)+37
  System.Web.UI.WebControls.LinkBut​​ton.OnCommand(CommandEventArgs   e)+125
  System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(字符串   eventArgument)+169
  System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串   eventArgument)+9
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler   sourceControl,String eventArgument)   +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection   postData)+176
  System.Web.UI.Page.ProcessRequestMain(布尔   includeStagesBeforeAsyncPoint,Boolean   includeStagesAfterAsyncPoint)+5563

1 个答案:

答案 0 :(得分:0)

你为什么不设置

 EntityTypeName="JobLists"

然后在你的选择中选择jl?

即:

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e) 
{
    JobPostDataContext db = new JobPostDataContext();

    var query = from uM in db.aspnet_Memberships
                join uD in db.UserDetails
                on uM.UserId equals uD.UserId
                join u in db.aspnet_Users
                on uM.UserId equals u.UserId
                join jL in db.JobLists
                on uM.UserId equals jL.UserId
                where u.UserName == Request.QueryString["UserName"]

                select jl;


     e.Result = query;
}