2017-07-28 20 views
0

イムを使用して、多くの関係に多くのEntity Frameworkの内のレコードを複製します。 私は2つのエンティティを持っています:多対多の関係を持つ製品とサプライヤ。は、エンティティフレームワークとasp.net定型を使用してAsp.net定型

私の問題:私は1社のまたは複数のサプライヤーと製品を保存すると、製品およびsupplierProductテーブル上の関係が保存されているが、供給業者記録は、サプライヤーテーブルの上に複製されます。

私は、異なるコンテキストからの2つの実体があるので、それが起こる読んで、私は製品のコンテキストにサプライヤーを「添付」が必要です。私はそれをする方法を知らない。誰かが私を助けることができますか?

マイエンティティ:

public class Product : FullAuditedEntity 
{ 

    public Product() 
    { 
     Suppliers = new HashSet<Supplier>(); 
    } 

    public string Name { get; set; } 

    public virtual ICollection<Supplier> Suppliers { get; set; } 
} 


public class Supplier : FullAuditedEntity 
{ 
    public Supplier() 
    { 
     Products = new HashSet<Product>(); 
    } 

    public string Name { get; set; } 

    public virtual ICollection<Product> Products { get; set; } 
} 

プロダクトマネージャーと呼ばれるマイドメインサービス

public async Task<Product> Create(Product entity) 
    { 
     var product = _repositoryProduct.FirstOrDefault(x => x.Id == entity.Id); 

     if (product != null) 
     { 
      throw new UserFriendlyException("Product already exists."); 
     } 
     else 
     { 
      return await _repositoryProduct.InsertAsync(entity); 
     } 
    } 

ProductAppServiceと呼ばれる自分のアプリケーション・サービス:

public async Task Create(CreateProductInput input) 
    { 
     Product output = Mapper.Map<CreateProductInput, Product>(input); 
     await _productManager.Create(output); 
    } 

マイCreateProductInputデータ転送オブジェクト

public class CreateProductInput 
{ 
    public string Name { get; set; } 

    public ICollection<Supplier> Suppliers { get; set; } 
} 

マイ角度成分の製品リスト成分

// GET Products 
    function getProducts() { 
     productService.listAll() 
      .then(function (result) { 
       vm.users = result.data; 
      }); 
    } 
    getProducts(); 

    // GET Suppliers 
    function getSuppliers() { 
     supplierService.listAll() 
      .then(function (result) { 
       vm.suppliers = result.data; 
      }); 
    } 
    getSuppliers(); 

    //Save the data 
    vm.save = function() { 
     abp.ui.setBusy(); 
     productService.create(vm.product) 
      .then(function() { 
       abp.notify.info(App.localize('SavedSuccessfully')); 
       $uibModalInstance.close(); 
      }).finally(function() { 
       abp.ui.clearBusy(); 
       getProducts(); 
      }); 
    } 
+0

最も関連性の高いメソッド実装 - 'Product'リポジトリの' InsertAsync'は含まれていません。 –

+0

asp.netの定型句のメソッドhttps://github.com/aspnetboilerplate/aspnetboilerplate –

+1

デフォルトの汎用リポジトリの実装はまったく未知であり、関連データを持つエンティティでは機能しません。ほとんどの場合、カスタムリポジトリを実装する必要があります。 –

答えて

0

を重複していないする必要があり、そのようにそれらを取得することができます。私はカスタムリポジトリを実装し、そこにdbcontextに自分のエンティティを添付しました。

This linkは、カスタムリポジトリを作成する方法を詳細に示しています。レジュームにおいて

1-がメソッドを作成し、カスタム・リポジトリ+

3-実装し、コンテキストにエンティティのthatsを取り付けるdbcontext.attach(entity)を使用するカスタムリポジトリINTERFACE

2-作成コンテキストを複製して保存していました。

4-カスタムリポジトリのドメインサービスで使用IRepositoryを交換してください。

5カスタム作成したメソッドを使用して幸せになります。

メッセージが十分に明確でない場合はここに入力してください。

0

私はこの問題は、法と何がそれに渡すを作成中に何が起こるかだと思います。 CreateProductInputは、Idプロパティを持っていないので、それは常に他のブロックに陥ると

ザッツ新しいエンティティを挿入しますなぜあなたはメソッドを作成するために渡すパラメータは常にそれゆえイド== 0を持つことになりますように見える私に

public async Task<Product> Create(Product entity) 
{ 
    var product = _repositoryProduct.FirstOrDefault(x => x.Id == entity.Id); 

    if (product != null) 
    { 
     throw new UserFriendlyException("Product already exists."); 
    } 
    else 
    { 
     return await _repositoryProduct.InsertAsync(entity); 
    } 
} 

からマップするしたがって、常にId(int = 0)のデフォルト値を持つプロダクトにマップされます。

また、あなたがあなたのサプライヤーentitesのidを渡すと、作成呼び出す前に、それらを自動的にDbContextに取り付けられており、私はそれを解決する方法を見つけ出すイワンStoevのコメントに続いて

+1

ちょっとユーザー、tkxが答えです。私はこの問題をすぐに解決する方法を見つける –

関連する問題