1

エンティティ フレームワークで asp.net ボイラープレートを使用しています。多対多の関係を持つ製品とサプライヤーの 2 つのエンティティがあります。

私の問題: 1 つまたは複数のサプライヤーで製品を保存すると、製品とsupplierProduct テーブルの関係は保存されますが、サプライヤー レコードはサプライヤー テーブルに複製されます。

異なるコンテキストのエンティティが 2 つあるために発生することを読んだので、サプライヤーを Products コンテキストに「アタッチ」する必要があります。やり方がわかりません。誰かが私を助けることができますか?

私のエンティティ:

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

ProductManager という私のドメイン サービス

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

私のAngularコンポーネントproduct-list-component

    // 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();
            });
    }
4

2 に答える 2