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; } 

    ... 
}