locked
Getting NullReferenceException on object deletion. RRS feed

  • Question

  • User949231861 posted

    I'm getting a null reference exception when trying to delete items from both the Details or List pages. This seems to only happen for one of my tables. I've checked and it is not a foreign key constraint issue where another table is dependent on the key or is it a varchar key like another topic I found on this forum. This is something that only comes up once published and running on the server as it does not happen while running the site on my development machine. Here is the stack trace produced:

    [NullReferenceException: Object reference not set to an instance of an object.]
       System.Data.Objects.Internal.ForeignKeyFactory.CreateConceptualNullKey(EntityKey originalKey) +16
       System.Data.Objects.DataClasses.EntityReference.NullAllForeignKeys() +1111
       System.Data.Objects.EntityEntry.NullAllForeignKeys() +275
       System.Data.Objects.EntityEntry.Delete(Boolean doFixup) +95
       System.Data.Objects.ObjectContext.DeleteObject(Object entity) +149
       System.Web.UI.WebControls.EntityDataSourceView.ExecuteDelete(IDictionary keys, IDictionary oldValues) +140116
       System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +3702789
       System.Web.UI.WebControls.FormView.HandleDelete(String commandArg) +872
       System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +675
       System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source, EventArgs e) +148
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +52
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707
    Monday, October 29, 2012 9:19 PM

Answers

  • User-330204900 posted

    OK I know what the issue may be, I have had issues where I was using char(n) as the FK->PK relationship is that what you are doing as I know this is fixed in .Net 4.5

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 31, 2012 3:28 PM

All replies

  • User-330204900 posted

    This is usually when deleting an entity that is still being referenced from another entity?

    Tuesday, October 30, 2012 8:12 AM
  • User949231861 posted

    Happens with both referenced and unreferenced entities from just the one table. I actually found the error when adding an entity then trying to delete it right afterwards. Past that even, the referenced part should not be an issue since the database has it set to cascade the delete, and that functionality works when deleting from other tables set up the same way.

    Tuesday, October 30, 2012 1:15 PM
  • User3866881 posted

    set to cascade the delete, and that functionality works when deleting from other tables set up the same way.

    Hello,

    Is your problem solved when setting the cascading key?

    Tuesday, October 30, 2012 9:34 PM
  • User-330204900 posted

    Hi Cody, can you send me a screen shot of your data model showing the table and any related tables please (to my e-mail address)

    Wednesday, October 31, 2012 6:48 AM
  • User949231861 posted

    Good news, the bug seems to have gone away after upgrading everything to using .net 4.5 and Entity Framework 5.0. I cannot replicate it on the deployment server.

    Wednesday, October 31, 2012 2:40 PM
  • User-330204900 posted

    OK I know what the issue may be, I have had issues where I was using char(n) as the FK->PK relationship is that what you are doing as I know this is fixed in .Net 4.5

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 31, 2012 3:28 PM
  • User949231861 posted

    No the primary key was an integer identity key.

    Wednesday, October 31, 2012 3:38 PM
  • User3866881 posted

    Good news, the bug seems to have gone away after upgrading everything to using .net 4.5 and Entity Framework 5.0. I cannot replicate it on the deployment server.

    Hi,

    I come here to close the issue. If you have futher information, you can continue by creating another new thread.

    Wednesday, October 31, 2012 8:18 PM