updateing products via plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hi Guys,

i have a plugin which imports products from an external provider.
this can be an insert or an update. now what i encounter is that sometimes for some products when i'm updating i get the message :

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.

i'm only updating a few fields like stock, weight, updatedonutc, etc.
but even if i don't update anything , or 1 field i get the error.

i'm using simply
Core.Domain.Catalog.Product() productDB = _productService.GetProductBySku(product.ProductCode);
...
_productService.UpdateProduct(productDB);

do you know what could be?
6 years ago
Hello,

Can you share full stack error from inner exception ?

Thanks,
Jatin
6 years ago
forefront wrote:
Hello,

Can you share full stack error from inner exception ?

Thanks,
Jatin


sure:

System.InvalidOperationException: 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.
   at System.Data.Entity.Core.Objects.ObjectContext.PrepareToSaveChanges(SaveOptions options)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
   at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()
   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   at System.Data.Entity.DbContext.SaveChanges()
   at Nop.Data.EfRepository`1.Insert(T entity) in \Libraries\Nop.Data\EfRepository.cs:line 81
   at Nop.Services.Catalog.CategoryService.InsertProductCategory(ProductCategory productCategory) in \Libraries\Nop.Services\Catalog\CategoryService.cs:line 541
   at Nop.Plugin.Misc.PL.Controllers.PLController.HandleProduct(ProductModel product, String type, List`1 allCategoriesFromDB, List`1 allProductsFromDB, IList`1 allCustomerRolesFromDB, SpecificationAttribute InternalIdAttribute, SpecificationAttribute VATIdAttribute, SpecificationAttribute VATPercentAttribute)
   at Nop.Plugin.Misc.PL.Controllers.PLController.SyncCategoriesAndProducts()
6 years ago
Post the constructor for current controller.
6 years ago
forefront wrote:
Post the constructor for current controller.


        public PLController(IRepository<PLRecord> repo,
            IRepository<PLProductRecord> productRepo,
            IRepository<PLPartnerRecord> partnerRepo,
            IRepository<PLOrderRecord> orderRepo,
            ISettingService settingService,
            IPermissionService permissionService,
            IStoreContext storeContext,
            IStoreService storeService,
            IWorkContext workContext,
            IPluginFinder pluginFinder,
            ILocalizationService localizationService,
            IProductService productService,
            ICategoryService categoryService,
            IUrlRecordService urlRecordService,
            ICurrencyService currencyService,
            IPictureService pictureService,
            ICustomerService customerService,
            ISpecificationAttributeService specificationAttributeService,
            IOrderService orderService,
            IEncryptionService encryptionService,
            CustomerSettings customerSettings,
            INewsLetterSubscriptionService newsLetterSubscriptionService,
            IGenericAttributeService genericAttributeService,
            ICustomerAttributeParser customerAttributeParser,
            ICustomerAttributeService customerAttributeService,
            IAddressAttributeParser addressAttributeParser,
            IAddressAttributeService addressAttributeService,
            IAddressService addressService,
            ICountryService countryService)
        {
            _productRepo = productRepo;
            _repo = repo;
            _partnerRepo = partnerRepo;
            _orderRepo = orderRepo;
            _settingService = settingService;
            _permissionService = permissionService;
            _storeContext = storeContext;
            _storeService = storeService;
            _workContext = workContext;
            _pluginFinder = pluginFinder;
            _localizationService = localizationService;
            _productService = productService;
            _categoryService = categoryService;
            _urlRecordService = urlRecordService;
            _currencyService = currencyService;
            _pictureService = pictureService;
            _customerService = customerService;
            _specificationAttributeService = specificationAttributeService;
            _orderService = orderService;
            _encryptionService = encryptionService;
            _customerSettings = customerSettings;
            _newsLetterSubscriptionService = newsLetterSubscriptionService;
            _genericAttributeService = genericAttributeService;
            _customerAttributeParser = customerAttributeParser;
            _customerAttributeService = customerAttributeService;
            _addressAttributeParser = addressAttributeParser;
            _addressAttributeService = addressAttributeService;
            _addressService = addressService;
            _countryService = countryService;
        }
6 years ago
forefront wrote:
Post the constructor for current controller.


did you find anything unusual?
thanks!
6 years ago
FYI - i'm using version 3.8, any help would be appreciated.
6 years ago
It seems to be related to the Categories:

..., the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
...
   at Nop.Services.Catalog.CategoryService.InsertProductCategory(ProductCategory productCategory) in


Are you doing anything with the product's categories?
6 years ago
hello,

i'm not doing anything with the category.
Even if i'm not changing anything, i get the error.

if i simply do a


var productDB = _productService.GetProductBySku(product.ProductCode);

...

_productService.UpdateProduct(productDB);



the error is there. (of course i want to change things, after this error is corrected)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.