1

我的原始问题是here使用EF信息库从聚合根中删除子对象

下面是我更新的代码。

Public Function StockTransferItemRemove(removeRequest As StockTransferItemRequest) As StockTransferItemResponse Implements IStockTransferService.StockTransferItemRemove 
     ' create your objects 
     Dim removeResponse = New StockTransferItemResponse 
     Dim stockTransfer As New StockTransfer 
     Dim stockTransferItem As New StockTransferItem 

     Try 

      ' get the aggregate root 
      stockTransfer = _stockTransferRepository.FindBy(removeRequest.StockTransferID).FirstOrDefault 

      stockTransfer.RemoveItem(removeRequest.StockTransferItemView.Id) 

      _stockTransferRepository.Save(stockTransfer) 

      Dim count As Integer = _uow.WMSCommit() 

      If (count > 0) Then 
       ' the object was saved succesfully 
        removeResponse.Success = True 
      Else 
       ' the object was not saved successfully 
       removeResponse.BrokenRules.Add(New BusinessRule(String.Empty, String.Empty, Tags.Messages.Commit_Failed)) 
      End If 


     Catch ex As Exception 
      ' an unexpected error occured 
      removeResponse.BrokenRules.Add(New BusinessRule(String.Empty, String.Empty, ex.Message)) 
     End Try 

     Return removeResponse 
    End Function 

当工作单元尝试提交时,会产生以下错误消息。

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. 

我知道,当我使用StockTransfer.RemoveItem(),它从集合中删除的项目,但它使记录在数据库中,这就是为什么我收到错误。

有没有办法从聚合Root中删除子对象并保持聚合根?

回答

0

对不起,不明确的代码,但我是一个C#人,所以试图找到我的方式在VB代码。您应该在要清除的实体链接上使用.Clear()选项。

实施例:

公司<>员工

Company.Emplyees.Clear()去除在关系 表的所有记录。

+0

我不想删除关系表中的所有记录,只是特定的项目。 – user1180223 2012-02-03 15:40:32

0

这是一个问题我也有。我不知道纯粹的解决方案,但我总是必须在保存更改之前在ef上手动删除它。在保存的库方法中,您应该检查在ef上下文中但不在聚合集合中的实体,并将它们从上下文中的dbset中移除。

+0

您能否提供一个简单的保存方法示例?此刻,我只将我的实体(这是我的聚合根)传递给我的保存方法。我需要通过其他任何东西吗? – user1180223 2012-02-17 09:15:01

0

您是否找到一个好的解决方案?我使用ParentAttribute创建了一个解决方案,并扩展了DbContext SaveChanges或ValidateEntity。你可以找到我的解决方案here

0

答案可能有点晚,但是,我的数据上下文中的这个扩展方法名为DataContext(它继承自DbContext)使用EF4.3为我工作。

public static void Delete<TEntity>(this DataContext context, IEnumerable<TEntity> entities) where TEntity : class, new() 
{ 
    foreach (var entity in entities) 
    { 
     context.Delete(entity); 
    } 
} 

public static void Delete<TEntity>(this DataContext context, TEntity entity) where TEntity : class, new() 
{ 
    var obj = context.Entry(entity); 
    if (obj.State == System.Data.EntityState.Detached) 
    { 
     context.Set(typeof(TEntity)).Attach(obj.Entity); 
    } 
    context.Set(typeof(TEntity)).Remove(obj.Entity); 
} 

而数据上下文类只是为了完整性。

public class DataContext : DbContext 
{ 
    public DbSet<MyPOCO> POCOs { get; set; } 

    ... 
}