如何在 nopCommerce 的 Category.cs 类中添加实体?

问题描述 投票:0回答:0

我想在 nopCommerce 的类别表中添加实体类别类型。对于此实现,我遵循nopCommerce/开发人员教程并根据此链接实现。

第 1 步:我在 Nop.Core.Domain.Category.cs 类中添加了新字段 CategoryType。

namespace Nop.Core.Domain.Catalog
{
    /// <summary>
    /// Represents a category
    /// </summary>
    public partial class Category : BaseEntity, ILocalizedEntity, ISlugSupported, IAclSupported, IStoreMappingSupported, IDiscountSupported<DiscountCategoryMapping>, ISoftDeletedEntity
    {
        public string CategoryType { get; set; }
        /// <summary>
        /// Gets or sets the name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the description
        /// </summary>
    }
}

第 2 步:我在 Nop.Data.Mapping.Builders.Catalog CategoryBuilder.cs 类的映射器类中映射 CategoryType 列,如下所述:

public override void MapEntity(CreateTableExpressionBuilder table)
{
    table
        .WithColumn(nameof(Category.CategoryType)).AsString(400).NotNullable()
        .WithColumn(nameof(Category.Name)).AsString(400).NotNullable()
        .WithColumn(nameof(Category.MetaKeywords)).AsString(400).Nullable()
        .WithColumn(nameof(Category.MetaTitle)).AsString(400).Nullable()
        .WithColumn(nameof(Category.PageSizeOptions)).AsString(200).Nullable();
}

第 3 步:现在我还创建了迁移类,用于将新列迁移到类别表中。 为此,我在命名空间 Nop.Data.Migrations 中设计了名为 AddCategoryType.cs 的新类:

public class AddCategoryType : AutoReversingMigration
{
    //Add migration for Add Category Type in Category table.
    public override void Up()
    {
        Create.Column(nameof(Category.CategoryType))
         .OnTable(nameof(Category))
         .AsString(255)
         .Nullable();
    }
}

第四步:验证器类用于验证存储在模型类内部的数据 为了应用验证,我在命名空间 Nop.Web.Areas.Admin.Validators.Catalog:

中添加了类 CategoryValidator.cs
public partial class CategoryValidator : BaseNopValidator<CategoryModel>
{
    public CategoryValidator(ILocalizationService localizationService, IMappingEntityAccessor mappingEntityAccessor)
    {
        RuleFor(x => x.CategoryType).Length(0, 255);
        RuleFor(x => x.CategoryType).NotEmpty().WithMessageAwait(localizationService.GetResourceAsync("Admin.Catalog.Categories.Fields.CategoryType.Required"));
    }
}

第 5 步:我在视图侧添加了 CateoryType 列。为此,我在命名空间

Nop.Web.Areas.Admin.Models.Catalog
:

的 CategoryModel.cs 类中添加了 CategoryType 列
public partial record CategoryModel 
{
    [NopResourceDisplayName("Admin.Catalog.Categories.Fields.CategoryType")]
    public string CategoryType { get; set; }
}

第 6 步:现在我在视图中进行更改,以在命名空间 nop.Web.Areas.Admin.Views.Category 中的类 _CreateOrUpdate.Info.cshtml 中显示这个新添加的列:

nop.Web.Areas.Admin.Views.Category._CreateOrUpdate.Info.cshtml

@model CategoryModel

<div class="card-body">
    @(await Html.LocalizedEditorAsync<CategoryModel, CategoryLocalizedModel>("category-name-localized",
        @<div>
<div class="form-group row">
            <div class="col-md-3">
                <nop-label asp-for="CategoryType" />
            </div>
            <div class="col-md-9">
                <nop-editor asp-for="CategoryType" asp-required="true" />
                <span asp-validation-for="CategoryType"></span>
            </div>
        </div>
        <div class="form-group row">
            <div class="col-md-3">
                <nop-label asp-for="Description" />
            </div>
            <div class="col-md-9">
                <nop-editor asp-for="Description" asp-template="RichEditor" />
                <span asp-validation-for="Description"></span>
            </div>
        </div>
    </div>))
</div>

我实现的此代码未在类别表中创建类别类型并在输出中显示错误:处理请求时发生未处理的异常。 SqlException:列名“CategoryType”无效。

c# asp.net-mvc testing nopcommerce developer-tools
© www.soinside.com 2019 - 2024. All rights reserved.