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を(使用したとき、私はそれがコレクションから項目を削除しますが、それは、私はエラーが発生します理由はデータベースに記録を保持することを)知っています。

子オブジェクトを集約ルートから削除し、集約ルートを永続化する方法はありますか?

答えて

0

私はVBコードで自分のやり方を見つけようとしています。クリアするエンティティリンクで.Clear()オプションを使用する必要があります。

例:

会社<>社員

Company.Emplyees.Clear()関係 テーブルのすべてのレコードを削除します。

+0

私は関係テーブル内のすべてのレコード、単に特定の項目を削除したいわけではありません。 – user1180223

0

これも問題があります。私は純粋な解決策はわかりませんが、変更を保存する前に常にefのコンテキストで削除する必要があります。保存のためのリポジトリメソッドでは、efコンテキストには存在し、集約コレクションには存在しないエンティティをチェックし、コンテキスト上のdbsetからエンティティを削除する必要があります。

+0

保存方法の簡単な例を教えてください。現時点では、私の集約ルートである自分のエンティティを私のsaveメソッドに渡すだけです。私は他の何かを通過する必要がありますか? – user1180223

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

    ... 
} 
関連する問題