【问题标题】:Delete using navigation properties in EF 6使用 EF 6 中的导航属性删除
【发布时间】:2015-01-30 08:43:25
【问题描述】:

我在我的应用程序中使用 EF 6 数据库模式。我有一个表TBL_USER,它在其他表中有1:N 关系。其中之一是TBL_USER_CASE,其中TBL_USER 的主键在TBL_USER_CASE 中充当外键。

现在我要从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;
    }

我在这里遇到异常

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 不,不是
  • 通常错误会非常详细地显示问题所在。此错误描述在您的操作之后必须存在一些关系不稳定:例如,1:N 关系没有匹配的1。
  • 您可能想尝试内部异常以确定导致此问题的字段。如果异常是 DbValidationException 类型,请检查此链接以获取更多详细信息:stackoverflow.com/questions/5345890/…
  • 顺便说一句:因为你的函数被称为DeleteUser;你为什么不删除用户?如果这是您的目标,您可以依靠 cascading deletes 删除子关系。

标签: c# asp.net entity-framework-6


【解决方案1】:

好的,如果您的目标是删除用户,您可以让框架处理子关系。你可以试试这个:

private long DeleteUser(long UserID)
{
    using(VerbaTrackEntities dataContext = new VerbaTrackEntities())
    {
        TBL_USER user = dataContext.TBL_USER.
                                 SingleOrDefault(x => x.LNG_USER_ID == UserID);
        if(user != null)
        {       
            dataContext.TBL_USER.Remove(user); 
            dataContext.SaveChanges();
        }
    }
    return 0;
}

更新:你能试试这个吗:

private long DeleteUser(long UserID)
{
    using(VerbaTrackEntities dataContext = new VerbaTrackEntities())
    {
        TBL_USER user = dataContext.TBL_USER
                             .SingleOrDefault(x => x.LNG_USER_ID == UserID);
        if(user != null)
        {
            foreach (var cases in user.TBL_USER_CASE.ToList())
            {
                //little modification is here
                dataContext.TBL_USER_CASE.Remove(cases);                          
            }
        }
        dataContext.SaveChanges();
    }
    return 0;
}

【讨论】:

  • 不会抛出外键违规错误吗?
  • @RajeevKumar:如果您没有更改任何cascading delete 选项,它也应该删除与外键相关的条目。
  • 发生错误{"The DELETE statement conflicted with the REFERENCE constraint \"USRCAS_USR_FK\". The conflict occurred in database \"VerbaTrack\", table \"dbo.TBL_USER_CASE\", column 'LNG_USER_ID'.\r\nThe statement has been terminated."}
  • @RajeevKumar:嗯,我没有怀疑那个:|我发布了最后一次尝试。如果它不起作用,也许您也可以发布实体 TBL_USER 和 TBL_USER_CASE。
【解决方案2】:

我自己通过网络阅读来做到这一点。我已经这样做了

 System.Data.Entity.Core.Objects.ObjectContext oc = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)dataContext).ObjectContext;
 foreach(var Cases in user.TBL_USER_CASE.ToList())
    {                        
       oc.DeleteObject(Cases);                       
    }                    
    oc.SaveChanges();
    dataContext.TBL_USER.Remove(user);

【讨论】:

    猜你喜欢
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多