Hello,
With nopCommerce_1.80, SQL Server2008
I write a API in nop which call the function ProductManager.InsertProduct and ProductManager.UpdateProducte
public void AddNewProduct(ProductRequest productRequest)
{
Product product = null;
product = SaveProductInfo(productRequest);
//save SEO, Category, Manufacturer info
SaveProductSEO(product.ProductId, productRequest);
//SaveProductCategory(product.ProductId, productRequest);
//SaveProductManufacturer(product.ProductId, productRequest);
//SaveProductPicture(product.ProductId, productRequest);
}
for my method SaveProductInfor(productRequest) -> using parameter instead of parameter from WebUI.
Then i have a application WinForm to call the API
api = new ProductAPI();
request.name = "insert by api";
request.shortDescription = "shortDescription";
request.fullDescription = "fullDescription";
request.adminComment = "adminComment";
request.manufacturerPartNumber = "";
request.templateId = 4;
request.sku = "";
request.userAgreementText = "userAgreementText";
request.productTagNames = new string[] { "Laptop", "Lenovo", "G430" };
request.published = true;
request.allowCustomerRatings = true;
request.allowCustomerReviews = true;
request.showOnHomePage = true;
//SEO
request.metaKeywords = "metaKeywords";
request.metaDescription = "metaDescription";
request.metaTitle = "metaTitle";
request.SEName = "SEName";
//end SEO
try
{
api.AddNewProduct(request);
}
catch (Exception ex)
{
}
api.AddNewProduc will call
1. product = SaveProductInfo(productRequest) -> will call ProductManager.InsertProduct ; -> it worked fine
2. then SaveProductSEO(product.ProductId, productRequest); -> call ProductManager.UpdateProduct
-> An exceptoin raise in ProductManager.UpdateProduct at methode context.IsAttached(product) line 534
the return false
3. Exception at context.IsAttached(product) when using entry = context.ObjectStateManager.GetObjectStateEntry(entity);
The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'System.Data.Entity.DynamicProxies.Product_41AF00E728CA9C12156EFA331953CC0F4B76565227D385B09148EA61519D03B2'.
4. -> So in ProductManager.UpdateProduct will call context.Products.Attach(product); raise an exception
System.InvalidOperationException: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
at System.Data.Objects.ObjectContext.VerifyContextForAddOrAttach(IEntityWrapper wrappedEntity)
at System.Data.Objects.ObjectContext.AttachSingleObject(IEntityWrapper wrappedEntity, EntitySet entitySet, String argumentName)
at System.Data.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
at System.Data.Objects.ObjectSet`1.Attach(TEntity entity)
at NopSolutions.NopCommerce.BusinessLogic.Products.ProductManager.UpdateProduct(Int32 productId, String name, String shortDescription, String fullDescription, String adminComment, Int32 templateId, Boolean showOnHomePage, String metaKeywords, String metaDescription, String metaTitle, String seName, Boolean allowCustomerReviews, Boolean allowCustomerRatings, Int32 ratingSum, Int32 totalRatingVotes, Boolean published, Boolean deleted, DateTime createdOn, DateTime updatedOn) in D:\Projects\4-OlalaWeb\Dev\nopCommerce_1.80_Source\Libraries\Nop.BusinessLogic\Products\ProductManager.cs:line 535
Note: I don't change source code of methods Products.ProductManager.UpdateProduct and Products.ProductManager.InsertProduct
Thanks for reading