Change entity mapping

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 年 前
Hello!

I'm trying to implement XML import option to my nopCommerce store.
XML has nice integer ID's for categories, so I turned off autonumber option for dbo.Category table in database.
Now, I'm getting exception:
Cannot insert the value NULL into column 'Id', table 'nopCommerce.dbo.Category'; column does not allow nulls. INSERT fails.
The statement has been terminated.

My XML:
<cat>
<id>17</id>
<name>Category name</name>
<parent_id>0</parent_id>
</cat>

My code:
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);
        }
11 年 前
Try adding this on the CategoryMap class:

nopcommerce\src\Libraries\Nop.Data\Mapping\Catalog\CategoryMap.cs

this.Property(c => c.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);


Don't forget to add the using statement:


using System.ComponentModel.DataAnnotations;
11 年 前
insomnium_ wrote:
I turned off autonumber option for dbo.Category table in database.


And do you turn it back on when you've finished the import??  (if not, what happens when you add a new category in Admin?)
11 年 前
I have too much categories, to add them manually, so I disabled manual category creating option
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.