我從XML函數創建類別的導入。 首先,使用XDocument創建要添加的類別列表。我關閉了Categories表中的ID的isIdentity選項,因爲我打算使用XML中的ID。nopCommerce插入實體失敗
XML例如:
<cat>
<id>17</id>
<name>Category name</name>
<parent_id>0</parent_id>
</cat>
然後,我寫的方法,它試圖通過ID和更新以獲得類別,或插入新:
var category = _categoryService.GetCategoryById(Id);
if (category != null)
{
category.Name = model.Name;
category.ParentCategoryId = model.ParentCategoryId;
category.UpdatedOnUtc = DateTime.UtcNow;
category.Published = true;
category.Deleted = false;
_categoryService.UpdateCategory(category);
}
else
{
category = new Core.Domain.Catalog.Category();
category.Id = model.Id;
category.ParentCategoryId = model.ParentCategoryId;
category.Name = model.Name;
category.UpdatedOnUtc = DateTime.UtcNow;
category.CreatedOnUtc = DateTime.UtcNow;
category.Published = true;
category.Deleted = false;
_categoryService.InsertCategory(category);
}
然後就是最怪異 - 應用程序拋出異常:無法將值NULL插入'Id'列,'nopCommerce.dbo.Category'表;列不允許有空值。 INSERT失敗。 該聲明已被終止。 但即使在調試器中,類別ID也不爲空。請求幫助! 在此先感謝!
更新:InsertCategory是一個標準的nopCommerce方法:
/// <summary>
/// Inserts category
/// </summary>
/// <param name="category">Category</param>
public virtual void InsertCategory(Category category)
{
if (category == null)
throw new ArgumentNullException("category");
_categoryRepository.Insert(category);
//cache
_cacheManager.RemoveByPattern(CATEGORIES_PATTERN_KEY);
_cacheManager.RemoveByPattern(PRODUCTCATEGORIES_PATTERN_KEY);
//event notification
_eventPublisher.EntityInserted(category);
}
'公共無效插入(T實體) { 嘗試 { 如果(實體== NULL) 拋出新ArgumentNullException (「實體」); this.Entities.Add(entity); this._context.SaveChanges(); } catch(DbEntityValidationException dbEx) { throw dbex; } }' 是標準的nopCommerce方法。當我調試這個,實體不爲空,並有ID,這是最奇怪的事情... –
男人,我感到你的痛苦。希望您找到解決方案並將其作爲答案發布,以便其他人不會經歷這麼長時間的相同問題。也許向nopcommerce提交錯誤?乾杯 –