使用外键删除 LINQ 中的记录 - 无效转换异常
我有一个简单的数据库:
ARTICLE
----------
ArticleId (PK),
ArticleTitle
..other stuff...
USER-ARTICLE
------------
ArchiveId (PK),
UserId,
ArticleId
..other stuff...
articleId
是外键。
我希望能够使用以下代码通过 UserArticleId
删除用户文章行,
UserArticle myobjtodelete = PersonalArchiveDb.UserArticles.Single(ua => ua.ArchiveId == 3);
PersonalArchiveDb.UserArticles.DeleteOnSubmit(myobjtodelete);
PersonalArchiveDb.SubmitChanges();
(是的,我知道我可以在删除中执行该语句,而不是检索对象,它是用于调试目的,以确保对象确实存在 - 确实存在。)
当调试器点击 SubmitChanges()
行时,我收到运行时错误:
指定的演员阵容无效。
这是堆栈跟踪,
位于 System.Data.Linq.IdentityManager .StandardIdentityManager .SingleKeyManager`2.TryCreateKeyFromValues(Object[] 值, V& v) at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(对象[] 键值)在 System.Data.Linq.IdentityManager.StandardIdentityManager.Find(元类型 类型,Object[] keyValues)位于 System.Data.Linq.CommonDataServices.GetCachedObject(MetaType 类型, 对象[] keyValues)位于 System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, 对象实例)位于 System.Data.Linq.ChangeProcessor.BuildEdgeMaps() 在 System.Data.Linq.ChangeProcessor.SubmitChanges(冲突模式 故障模式)在 System.Data.Linq.DataContext.SubmitChanges(冲突模式失败模式)
在 System.Data.Linq.DataContext.SubmitChanges() 处 Driver_SOC_ASO.Controls.PersonalArchive.ArchiveListing.grdArchive_RowDeleting(对象 发送者、GridViewDeleteEventArgs e) 中 C:\work\Driver.Net\Driver-SOC-ASO\Driver-SOC-ASO\Controls\PersonalArchive\ArchiveListing.ascx.cs:line 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, 布尔值 CausesValidation、字符串验证组)位于 System.Web.UI.WebControls.GridView.RaisePostBackEvent(字符串 事件参数) at System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler) 源控件,字符串事件参数)位于 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint、布尔值 includeStagesAfterAsyncPoint)
我不知所措,有什么想法吗?
I have a simple database:
ARTICLE
----------
ArticleId (PK),
ArticleTitle
..other stuff...
USER-ARTICLE
------------
ArchiveId (PK),
UserId,
ArticleId
..other stuff...
The articleId
's are foreign keys.
I want to be able to delete a user article row by UserArticleId
using the following code,
UserArticle myobjtodelete = PersonalArchiveDb.UserArticles.Single(ua => ua.ArchiveId == 3);
PersonalArchiveDb.UserArticles.DeleteOnSubmit(myobjtodelete);
PersonalArchiveDb.SubmitChanges();
(Yes I'm aware I can do the statement inside the delete rather than retrieving the object, it was for debugging purposes to make sure the object definitely exists - it does.)
When the debugger hits the SubmitChanges()
line, I get a runtime error:
Specified cast is not valid.
Here is the stack trace,
at System.Data.Linq.IdentityManager
.StandardIdentityManager
.SingleKeyManager`2.TryCreateKeyFromValues(Object[] values, V& v) at
System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[]
keyValues) at
System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType
type, Object[] keyValues) at
System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type,
Object[] keyValues) at
System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc,
Object instance) at
System.Data.Linq.ChangeProcessor.BuildEdgeMaps() at
System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode
failureMode) at
System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges() at
Driver_SOC_ASO.Controls.PersonalArchive.ArchiveListing.grdArchive_RowDeleting(Object
sender, GridViewDeleteEventArgs e) in
C:\work\Driver.Net\Driver-SOC-ASO\Driver-SOC-ASO\Controls\PersonalArchive\ArchiveListing.ascx.cs:line
78 at
System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs
e) at System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow
row, Int32 rowIndex) at
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean
causesValidation, String validationGroup) at
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String
eventArgument) at
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
I am at a loss, any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试为
ArticleId
设置 ON CASCADE DELETEtry setting ON CASCADE DELETE for
ArticleId
在您的 dbml 中,检查是否存在不正确的黑白文章和用户文章关联。
In your dbml, check to see if there is incorrect an association b/w Article and UserArticle.
这可能是 Microsoft 所说的 bug 的一个示例已在 .NET 4.0 中修复。
This may be an example of this bug, which Microsoft says is fixed in .NET 4.0.