EF Code First中自引用实体的映射

本文关键字:实体 映射 自引用 Code First EF | 更新日期: 2023-09-27 18:27:50

在我的数据库中,我有一个表Category,包含列Id、CategoryName、ParentCategoryId,其中ParentCategory Id对Category.Id有约束。

我首先使用实体框架代码,其中实体看起来像:

public class Category
{
   public long Id { get; private set; }
   public string CategoryName { get; private set; }
   public long? ParentCategoryId { get; private set; }
   public Category ParentCategory { get; private set; }       
   public virtual ICollection<Category> SubCategories { get; private set; }
}

如果我试图对此运行查询,我会得到异常:

 The relationship 'ComplaintModel.FK_Complaint_Category' was not loaded because the type 'ComplaintModel.Category' is not available.'r'nThe following information may be useful in resolving the previous error:'r'nThe required property 'Category1' does not exist on the type 'EC.Complaint.Services.Command.Domain.Entities.Category'.'r'n'r'n"}    System.Exception {System.Data.MetadataException}

所以它似乎需要导航属性,如果我添加这些:

 public ICollection<Category> Category1 { get; private set; }
 public long? Category2Id { get; private set; }
 public Category Category2 { get; private set; }

查询有效。

当然,我不希望使用Category1和Category2属性,我希望使用ParentCategory和SubCategories属性。

如何首先告诉代码使用正确的导航属性?

EF Code First中自引用实体的映射

您的POCO类应该是这样的。。。

public class Category
{
   public long Id { get; private set; }
   public string CategoryName { get; private set; }
   public long? ParentCategoryId { get; private set; }
   public virtual Category ParentCategory { get; private set; }       
   public virtual ICollection<Category> SubCategories { get; private set; }
}
public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
    public CategoryConfiguration()
    {
        this.HasKey(x => x.Id);
        this.HasMany(category => category.SubCategories)
            .WithOptional(category => category.ParentCategoryId)
            .HasForeignKey(course => course.UserId)
            .WillCascadeOnDelete(false);
    }
}

Entity Framework 6处理此问题。您只需要确保[Key]注释用于标识主键。不确定它是否与虚拟关键字一起工作。

 public class Category
{
   [Key]
   public long Id { get; private set; }
   public string CategoryName { get; private set; }
   public long? ParentCategoryId { get; private set; }
   public Category ParentCategory { get; private set; }       
   public ICollection<Category> SubCategories { get; private set; }
}

我想我找到了它,我在OnModelCreating操作中添加了以下内容:

 modelBuilder.Entity<Domain.Entities.Category>().HasOptional<Category>(c => c.ParentCategory).WithMany().Map(m => m.MapKey(new string[] { "Id", "ParentCategoryId" }));

现在ParentCategory和SubCategories属性工作(我可以删除Category1和Category2)。但不知道SubCategories工作的确切原因。。。