EntityState.Deleted不起作用,删除(实体)呢? [英] EntityState.Deleted does not work, Remove(entity) does?

查看:1082
本文介绍了EntityState.Deleted不起作用,删除(实体)呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图读取记录,然后删除这些记录在同一个事务时挣扎的EF。我最初使用EntityState.Deleted方法,它会给出错误:

  

操作失败:关系无法改变,因为一个或一个以上的外键的属性是不可为空。当做出改变有关系,相关的外键属性设置为空值。如果外键不支持空值,一个新的关系必须定义,外键属性必须指定一个非空值,或者不相关的对象必须被删除。

但是,如果我改变它喜欢我下面,用卸下摆臂(),就万事大吉了。

  1. 的差异,最好的时间使用上卸下摆臂()VS .Deleted是什么?
  2. 我如何才能让使用.Deleted方法,这项工作?我曾尝试创建上下文到我的仓库的一个新实例来读取,另外删除,但随后得到了相关IEntityTracker无法跟踪多个实例的错误......我也试过在初始读取.INCLUDE加载相关记录进入EF所以它知道并删除它们。我也试过第一.Detaching读取记录。所有无济于事。

下面是有问题的方法。请注意,我确实有它使用.Deleted方法,已使我受益匪浅,直到这种情况下一个通用存储库(读然后删除相同的记录。)

  //删除配置需要和AllocatedContainers的页头需要的ID
公共ActionConfirmation< INT> DeleteAllocRecords(INT intFacilityId,AllocNeedSourceTypes needSourceType,INT intNeedSourceId)
{
VAR上下文=新InventoryMgmtContext();
VAR库=新AllocationNeedRepository(上下文);

//删除分配需要以及分配容器,因此儿童
VAR SRCTYPE = needSourceType.ToString();
名单< AllocationNeed> allocNeeds = repository.SearchFor(
    X => x.FacilityId == intFacilityId
    &功放;&安培; x.NeedSourceType == SRCTYPE
    &功放;&安培; x.NeedSourceId == intNeedSourceId
).ToList();

// VAR deleteRepository =新库< AllocationNeed>(); <  - 试图方面的单独的实例,删除...没有worky。

的foreach(AllocationNeed allocNeed在allocNeeds)
{
    尝试
    {
        //没有工作:context.Entry(allocNeed).State = System.Data.EntityState.Deleted;
        context.AllocationNeeds.Attach(allocNeed);
        context.AllocationNeeds.Remove(allocNeed); <  - 工程
        context.SaveChanges();
    }
    赶上(例外前)
    {
        返回ActionConfirmation< INT> .CreateFailureConfirmation(ex.Message,allocNeed.Id);
    }
}
 

解决方案

删除也将删除子对象,但使用删除不会。你真的应该使用删除出于这个原因。如果您真的要使用删除,你必须让你的外键可为空,但你最终会与孤立的记录(这是你不应该这样做,摆在首位的主要原因)之一。

I've been struggling with EF when trying to read records, then delete those records in the same transaction. I was initially using the EntityState.Deleted method, which would give an error:

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.

But if I change it to like I have below, using .Remove(), then all is well.

  1. What is the difference and best times to use .Remove() vs .Deleted?
  2. How could I make this work using the .Deleted method? I have tried creating a new instance of the context to my repository to read and another to delete, but then got errors related to IEntityTracker can't track multiple instances... I also tried .Include on the initial read to load the dependent records into EF so it knows about and deletes them. I also tried .Detaching the read records first. All to no avail.

Here is the method in question. Note that I do have a generic repository which uses the .Deleted method which has served me well until this scenario (reading then deleting the same records.)

//Delete Allocation Need and AllocatedContainers for alloc need id
public ActionConfirmation<int> DeleteAllocRecords(int intFacilityId, AllocNeedSourceTypes needSourceType, int intNeedSourceId)
{
var context = new InventoryMgmtContext();
var repository = new AllocationNeedRepository(context);

//Delete Allocation Need and hence children in Allocated Containers
var srcType = needSourceType.ToString();
List<AllocationNeed> allocNeeds = repository.SearchFor(
    x => x.FacilityId == intFacilityId
    && x.NeedSourceType == srcType
    && x.NeedSourceId == intNeedSourceId
).ToList();

//var deleteRepository = new Repository<AllocationNeed>(); <--tried separate instance of context to delete...no worky.

foreach (AllocationNeed allocNeed in allocNeeds)
{
    try
    {
        //NO WORK: context.Entry(allocNeed).State = System.Data.EntityState.Deleted;
        context.AllocationNeeds.Attach(allocNeed); 
        context.AllocationNeeds.Remove(allocNeed); <-- Works
        context.SaveChanges();
    }
    catch (Exception ex)
    {
        return ActionConfirmation<int>.CreateFailureConfirmation(ex.Message, allocNeed.Id);
    }
}

解决方案

Remove will also remove the child objects, but using Deleted will not. You should really be using Remove for this very reason. If you really want to use Deleted, you'd have to make your foreign keys nullable, but then you'd end up with orphaned records (which is one of the main reasons you shouldn't be doing that in the first place).

这篇关于EntityState.Deleted不起作用,删除(实体)呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆