我在我的应用程序中使用EF 6数据库模式。我有一个表TBL_USER
其中有1:N
关系在其他表中。其中之一是TBL_USER_CASE
,TBL_USER
的主键在TBL_USER_CASE
中充当外键。使用EF 6中的导航属性删除
现在我从TBL_USER
删除一些用户。在此之前,我需要删除TBL_USER_CASE
中的相应条目。我使用下面的代码为
private long DeleteUser(long UserID)
{
using(VerbaTrackEntities dataContext = new VerbaTrackEntities())
{
TBL_USER user = dataContext.TBL_USER.Where(x => x.LNG_USER_ID == UserID).SingleOrDefault();
if(user != null)
{
foreach (var cases in user.TBL_USER_CASE.ToList())
{
user.TBL_USER_CASE.Remove(cases);
}
}
dataContext.SaveChanges();
}
return 0;
}
在这里,我m到处例外
Additional information: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted
我怎么能这样做正确操作?
'TBL_USER_CASE'是另一个'1:N'关系的'N'吗? – Stefan 2014-12-02 07:28:07
@Stefan不,它不是 – 2014-12-02 07:30:49
正常情况下,这个错误通常会告诉你问题的细节。这个错误描述了你的行为之后必然存在一些相关的不一致:例如一个没有匹配的'1'的'1:N'关系。 – Stefan 2014-12-02 07:35:53