Table ID 字段自动附加 table 产生无效列名的名称

Table Id field automatically append with table name that produce invalid column name

我有 table Categories With Id column when insert occur is shows it shows error SqlException: Invalid column name 'CategoriesId'.

 public partial class Categories
    {
        public Categories()
        {
            CategoryTabs = new HashSet<CategoryTabs>();
        }

        public int Id { get; set; }
        public string Title { get; set; }
        public int? ParentId { get; set; }
        public int? SeasonId { get; set; }
        public int? Levels { get; set; }
        public string Image { get; set; }
        public string Description { get; set; }

        public virtual Seasons Season { get; set; }
        public List<Categories> children { get; set; }
        public virtual ICollection<CategoryTabs> CategoryTabs { get; set; }
    }

 public partial class CategoryTabs
    {
        public int Id { get; set; }
        public int? CategoryId { get; set; }
        public int? TabId { get; set; }
         public virtual Categories Category { get; set; }
        public virtual Tabs Tab { get; set; }
    }

CategoriesId 是与

引入的 one-to-many 自身关系关联的外键 property/column 的常规名称
public List<Categories> children { get; set; }

集合导航 属性 在 Categories 实体内。

查看实体模型,最有可能的想法是为此目的使用 ParentId。由于它不符合 EF Core 命名约定,因此必须使用 ForeignKey 数据注释显式映射它:

[ForeignKey(nameof(ParentId))]
public List<Categories> children { get; set; }

或在 OnModelCreating 内流利 API 覆盖:

modelBuilder.Entity<Categories>()
    .HasMany(e => e.children)
    .WithOne()
    .HasForeignKey(e => e.ParentId);