Deleting records in LINQ with foreign keys - invalid lithium exception

I have a simple database:

ARTICLE
----------
ArticleId (PK),
ArticleTitle
..other stuff...

USER-ARTICLE
------------
ArchiveId (PK),
UserId,
ArticleId
..other stuff...

articleId are foreign keys.

I want to delete a line of a user article using UserArticleId, using the following code,

UserArticle myobjtodelete = PersonalArchiveDb.UserArticles.Single(ua => ua.ArchiveId == 3);
PersonalArchiveDb.UserArticles.DeleteOnSubmit(myobjtodelete);
PersonalArchiveDb.SubmitChanges();

(Yes, I know that I can make an expression inside delete, and not retrieve the object, just for debugging purposes, make sure that the object definitely exists - it does.)

When the debugger hits the line SubmitChanges(), I get a runtime error:

The indicated deposit is not valid.

Here is the stack trace

System.Data.Linq.IdentityManager     .StandardIdentityManager     .SingleKeyManager`2.TryCreateKeyFromValues ​​(Object [] values, V & v) at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object [] keyValues) System.Data.Linq.IdentityManager.StandardIdentityManager.Find( , Object [] keyValues) System.Data.Linq.CommonDataServices.GetCachedObject( MetaType, [] keyValues) System.Data.Linq.ChangeProcessor.GetOtherItem( MetaAssociation, ) System.Data.Linq.ChangeProcessor.BuildEdgeMaps() System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failMode) System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
System.Data.Linq.DataContext.SubmitChanges() at Driver_SOC_ASO.Controls.PersonalArchive.ArchiveListing.grdArchive_RowDeleting ( , GridViewDeleteEventArgs e) C:\\Driver.Net\Driver-SOC-ASO\Driver-SOC-ASO\Controls\PersonalArchive\ArchiveListing.ascx.cs: 78 System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e) System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow , Int32 rowIndex) System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean Validation, String validationGroup) at System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

, ?

+3
3

ArticleId

0

In your dbml check if there is some association b / w Article and UserArticle.

0
source

Source: https://habr.com/ru/post/1699158/


All Articles