簡體   English   中英

使用Asp.net Boilerplate在Entity Framework中進行多對多關系復制記錄

[英]Duplicating record in Entity Framework many to many relationship using Asp.net Boilerplate

我在實體框架中使用asp.net樣板。 我有2個實體:具有多對多關系的產品和供應商。

我的問題:當我與一個或多個供應商保存產品時,會保存該產品及其在SupplierProduct表上的關系,但供應商記錄會在Supplier表上重復。

我讀到這種情況是因為有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; }
}

我的角度組件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();
            });
    }

我認為問題在於create方法中發生了什么以及您將傳遞給它什么。

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

在我看來,您傳遞給Create方法的參數始終具有Id == 0,因此為什么它將始終落入else塊並插入新實體

那是因為CreateProductInput沒有要映射的Id屬性。 因此,它將始終映射到具有默認值Id(int = 0)的產品。

另外,您可以傳遞供應商實體的ID並在調用Create之前獲取它們,這樣,這些實體應自動附加到DbContext上,而不要重復

根據Ivan Stoev的評論,我想出了解決方法。 我實現了一個自定義存儲庫,並將我的實體附加到那里的dbcontext中。

該鏈接詳細顯示了如何創建自定義存儲庫。

在簡歷中:

1-創建一個自定義的存儲庫接口

2-實施自定義存儲庫+

3-在dbcontext.attach(entity)創建一個方法,然后使用dbcontext.attach(entity)將復制的實體附加到上下文並保存該上下文。

4-將用於域服務的IRepository替換為自定義存儲庫。

5-使用自定義創建的方法,並感到高興。

如果消息不夠清晰,請在此處查看消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM