EntityDataSource throw Exception when Update with Self-Reference RRS feed

  • Question

  • User-2037814730 posted

    For a Table A defined in the DB like this:

    A : { id, selfId, name, ... }

    where selfId is a foreign key refered to Table A itself.

    I generated the Entity automatically like this:

    A : { string id; A self; string name, ... }

    when i update the entity in gridview Edit/Update CommandField

    i got the exception:

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace: 

    [NullReferenceException: Object reference not set to an instance of an object.]
       System.Data.Objects.DataClasses.RelatedEnd.GetOtherEndOfRelationship(IEntityWrapper wrappedEntity) +57
       System.Data.Objects.ObjectStateManager.AddEntityToCollectionOrReference(MergeOption mergeOption, IEntityWrapper wrappedSource, AssociationEndMember sourceMember, IEntityWrapper wrappedTarget, AssociationEndMember targetMember, Boolean setIsLoaded, Boolean relationshipAlreadyExists, Boolean inKeyEntryPromotion) +683
       System.Data.Objects.ObjectStateManager.PromoteKeyEntry(EntityEntry keyEntry, IEntityWrapper wrappedEntity, IExtendedDataRecord shadowValues, Boolean replacingEntry, Boolean setIsLoaded, Boolean keyEntryInitialized, String argumentName) +364
       System.Data.Objects.ObjectStateManager.FixupKey(EntityEntry entry) +11027329
       System.Data.Objects.EntityEntry.AcceptChanges() +205
       System.Data.Objects.ObjectContext.AcceptAllChanges() +279
       System.Web.UI.WebControls.EntityDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +624
       System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +3636597
       System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1336
       System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +641
       System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +164
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +52
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3691

    I guess maybe this is caused by:

    A is to be updated, and when the EntityDataSource construct the new A, the reference "self" is invalid at that moment.

    I do not want to modify my DB structure and do not want to lose the self reference,

    The event EntityDataSource_Updating is not triggered while the exception hit. 

    so i can not access my entity at any time.

    Maybe the worst idea is deal the whole Update Procedure myself.

    However, is there any better workaround?

    Wednesday, November 3, 2010 6:36 AM

All replies

  • User-2037814730 posted

    any support is welcome...

    Wednesday, November 3, 2010 9:56 PM
  • User1716267170 posted

    The problem happens when you update the row in Table A? Could you show us some details? Thanks.

    Monday, November 8, 2010 3:45 AM
  • User-2037814730 posted

    For an example, My data are like this in DB:

    Table A:

    Id Name ParentId
    c1 top NULL
    c2 good c1
    c3 bad c3

    And I generate Entity directly from the DB and i will have a Context class and class A with c1, c2, c3 properties.

    Then I create it an aspx page with a GridView and a EntityDataSource

    The EntityDataSource would be something like this:

        <asp:EntityDataSource ID="EntityDataSourceFields" runat="server" 
            DefaultContainerName="InfoPlusEntities" EnableUpdate="True" 
            Where="it.FormTemplate.FormTemplateId == @formTemplateId" 
            Include="CodeTable, Parent" onupdated="EntityDataSourceFields_Updated" 
            OrderBy="it.RepeatDepth ASC, it.GroupName ASC, it.FieldName ASC">
                <asp:QueryStringParameter Name="formTemplateId" Type="String"
                    QueryStringField="formTemplateId" />

        <asp:EntityDataSource ID="EntityDataSource1" runat="server" 

            ConnectionString="name=MyEntities"  DefaultContainerName="MyEntities" EnableUpdate="True" 

            EntitySetName="ASet" >


    and the GridView have attributes DataSourceID="EntityDataSource1" and a CommandField which ShowEditButton="True"

    When I click "Update" and the row turn into TextBoxes, and then I click "Save", Exception occurs for the 3rd row.

    row 1 and row 2 work fine.

    Wednesday, November 10, 2010 12:07 AM