2008-11-05 29 views
3

我有一个简单的数据库:删除记录与外键 - 无效的转换异常

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()线,我得到一个运行时错误:

Specified cast is not valid.

这里是堆栈跟踪,

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)

我很茫然,什么想法吗?

+0

您的示例代码使用“ArchiveId”而不是“ArticleId” - 在您的原始描述中,您的意思是“存档”而不是“文章”? – 2008-11-05 11:17:44

回答

0

尝试设置ON CASCADE删除ArticleId

1

这可能是这个bug的例子,微软表示,固定在.NET 4.0。

0

在您的dbml中,检查是否有不正确的关联b/w Article和UserArticle。