This error occurs when adding new category with no parent. I have tried similar question's answers but no result.
Model
public class Category
{
[Column("CategoryId")]
public Guid Id { get; set; }
[Required(ErrorMessage = "Category name is a required field.")]
[MinLength(1, ErrorMessage = "Minimum length for the Name is 1 characters.")]
public string Name { get; set; }
[ForeignKey("ParentId")]
public Guid? ParentId { get; set; }
public virtual Category Parent { get; set; }
public virtual ICollection<Category> Children { get; set; }
}
Request
public class CategoryCreationDto
{
public Guid ParentId { get; set; }
public string Name { get; set; }
}
Repository
public void Create(T entity) => RepositoryContext.Set<T>().Add(entity);
public void CreateCategory(Category category) => Create(category);
Controller
Category categoryEntity = _mapper.Map<Category>(category);
_repository.Category.CreateCategory(categoryEntity);
await _repository.SaveAsync();
Table generated by entity framework